tgsi/scan: fix num_inputs/num_outputs for shaders with overlapping arrays
[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_info.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_util.h"
44 #include "tgsi/tgsi_scan.h"
45
46
47 static bool
48 is_memory_file(unsigned file)
49 {
50 return file == TGSI_FILE_SAMPLER ||
51 file == TGSI_FILE_SAMPLER_VIEW ||
52 file == TGSI_FILE_IMAGE ||
53 file == TGSI_FILE_BUFFER;
54 }
55
56
57 /**
58 * Is the opcode a "true" texture instruction which samples from a
59 * texture map?
60 */
61 static bool
62 is_texture_inst(unsigned opcode)
63 {
64 return (opcode != TGSI_OPCODE_TXQ &&
65 opcode != TGSI_OPCODE_TXQS &&
66 opcode != TGSI_OPCODE_TXQ_LZ &&
67 opcode != TGSI_OPCODE_LODQ &&
68 tgsi_get_opcode_info(opcode)->is_tex);
69 }
70
71
72 /**
73 * Is the opcode an instruction which computes a derivative explicitly or
74 * implicitly?
75 */
76 static bool
77 computes_derivative(unsigned opcode)
78 {
79 if (tgsi_get_opcode_info(opcode)->is_tex) {
80 return opcode != TGSI_OPCODE_TG4 &&
81 opcode != TGSI_OPCODE_TXD &&
82 opcode != TGSI_OPCODE_TXF &&
83 opcode != TGSI_OPCODE_TXL &&
84 opcode != TGSI_OPCODE_TXL2 &&
85 opcode != TGSI_OPCODE_TXQ &&
86 opcode != TGSI_OPCODE_TXQ_LZ &&
87 opcode != TGSI_OPCODE_TXQS;
88 }
89
90 return opcode == TGSI_OPCODE_DDX || opcode == TGSI_OPCODE_DDX_FINE ||
91 opcode == TGSI_OPCODE_DDY || opcode == TGSI_OPCODE_DDY_FINE ||
92 opcode == TGSI_OPCODE_SAMPLE ||
93 opcode == TGSI_OPCODE_SAMPLE_B ||
94 opcode == TGSI_OPCODE_SAMPLE_C;
95 }
96
97
98 static void
99 scan_instruction(struct tgsi_shader_info *info,
100 const struct tgsi_full_instruction *fullinst,
101 unsigned *current_depth)
102 {
103 unsigned i;
104 bool is_mem_inst = false;
105 bool is_interp_instruction = false;
106
107 assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
108 info->opcode_count[fullinst->Instruction.Opcode]++;
109
110 switch (fullinst->Instruction.Opcode) {
111 case TGSI_OPCODE_IF:
112 case TGSI_OPCODE_UIF:
113 case TGSI_OPCODE_BGNLOOP:
114 (*current_depth)++;
115 info->max_depth = MAX2(info->max_depth, *current_depth);
116 break;
117 case TGSI_OPCODE_ENDIF:
118 case TGSI_OPCODE_ENDLOOP:
119 (*current_depth)--;
120 break;
121 default:
122 break;
123 }
124
125 if (fullinst->Instruction.Opcode == TGSI_OPCODE_INTERP_CENTROID ||
126 fullinst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
127 fullinst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
128 const struct tgsi_full_src_register *src0 = &fullinst->Src[0];
129 unsigned input;
130
131 is_interp_instruction = true;
132
133 if (src0->Register.Indirect && src0->Indirect.ArrayID)
134 input = info->input_array_first[src0->Indirect.ArrayID];
135 else
136 input = src0->Register.Index;
137
138 /* For the INTERP opcodes, the interpolation is always
139 * PERSPECTIVE unless LINEAR is specified.
140 */
141 switch (info->input_interpolate[input]) {
142 case TGSI_INTERPOLATE_COLOR:
143 case TGSI_INTERPOLATE_CONSTANT:
144 case TGSI_INTERPOLATE_PERSPECTIVE:
145 switch (fullinst->Instruction.Opcode) {
146 case TGSI_OPCODE_INTERP_CENTROID:
147 info->uses_persp_opcode_interp_centroid = TRUE;
148 break;
149 case TGSI_OPCODE_INTERP_OFFSET:
150 info->uses_persp_opcode_interp_offset = TRUE;
151 break;
152 case TGSI_OPCODE_INTERP_SAMPLE:
153 info->uses_persp_opcode_interp_sample = TRUE;
154 break;
155 }
156 break;
157
158 case TGSI_INTERPOLATE_LINEAR:
159 switch (fullinst->Instruction.Opcode) {
160 case TGSI_OPCODE_INTERP_CENTROID:
161 info->uses_linear_opcode_interp_centroid = TRUE;
162 break;
163 case TGSI_OPCODE_INTERP_OFFSET:
164 info->uses_linear_opcode_interp_offset = TRUE;
165 break;
166 case TGSI_OPCODE_INTERP_SAMPLE:
167 info->uses_linear_opcode_interp_sample = TRUE;
168 break;
169 }
170 break;
171 }
172 }
173
174 if (fullinst->Instruction.Opcode >= TGSI_OPCODE_F2D &&
175 fullinst->Instruction.Opcode <= TGSI_OPCODE_DSSG)
176 info->uses_doubles = TRUE;
177
178 for (i = 0; i < fullinst->Instruction.NumSrcRegs; i++) {
179 const struct tgsi_full_src_register *src = &fullinst->Src[i];
180 int ind = src->Register.Index;
181
182 /* Mark which inputs are effectively used */
183 if (src->Register.File == TGSI_FILE_INPUT) {
184 unsigned usage_mask;
185 usage_mask = tgsi_util_get_inst_usage_mask(fullinst, i);
186 if (src->Register.Indirect) {
187 for (ind = 0; ind < info->num_inputs; ++ind) {
188 info->input_usage_mask[ind] |= usage_mask;
189 }
190 } else {
191 assert(ind >= 0);
192 assert(ind < PIPE_MAX_SHADER_INPUTS);
193 info->input_usage_mask[ind] |= usage_mask;
194 }
195
196 if (info->processor == PIPE_SHADER_FRAGMENT) {
197 unsigned name, index, input;
198
199 if (src->Register.Indirect && src->Indirect.ArrayID)
200 input = info->input_array_first[src->Indirect.ArrayID];
201 else
202 input = src->Register.Index;
203
204 name = info->input_semantic_name[input];
205 index = info->input_semantic_index[input];
206
207 if (name == TGSI_SEMANTIC_POSITION &&
208 (src->Register.SwizzleX == TGSI_SWIZZLE_Z ||
209 src->Register.SwizzleY == TGSI_SWIZZLE_Z ||
210 src->Register.SwizzleZ == TGSI_SWIZZLE_Z ||
211 src->Register.SwizzleW == TGSI_SWIZZLE_Z))
212 info->reads_z = TRUE;
213
214 if (name == TGSI_SEMANTIC_COLOR) {
215 unsigned mask =
216 (1 << src->Register.SwizzleX) |
217 (1 << src->Register.SwizzleY) |
218 (1 << src->Register.SwizzleZ) |
219 (1 << src->Register.SwizzleW);
220
221 info->colors_read |= mask << (index * 4);
222 }
223
224 /* Process only interpolated varyings. Don't include POSITION.
225 * Don't include integer varyings, because they are not
226 * interpolated. Don't process inputs interpolated by INTERP
227 * opcodes. Those are tracked separately.
228 */
229 if ((!is_interp_instruction || i != 0) &&
230 (name == TGSI_SEMANTIC_GENERIC ||
231 name == TGSI_SEMANTIC_TEXCOORD ||
232 name == TGSI_SEMANTIC_COLOR ||
233 name == TGSI_SEMANTIC_BCOLOR ||
234 name == TGSI_SEMANTIC_FOG ||
235 name == TGSI_SEMANTIC_CLIPDIST)) {
236 switch (info->input_interpolate[input]) {
237 case TGSI_INTERPOLATE_COLOR:
238 case TGSI_INTERPOLATE_PERSPECTIVE:
239 switch (info->input_interpolate_loc[input]) {
240 case TGSI_INTERPOLATE_LOC_CENTER:
241 info->uses_persp_center = TRUE;
242 break;
243 case TGSI_INTERPOLATE_LOC_CENTROID:
244 info->uses_persp_centroid = TRUE;
245 break;
246 case TGSI_INTERPOLATE_LOC_SAMPLE:
247 info->uses_persp_sample = TRUE;
248 break;
249 }
250 break;
251 case TGSI_INTERPOLATE_LINEAR:
252 switch (info->input_interpolate_loc[input]) {
253 case TGSI_INTERPOLATE_LOC_CENTER:
254 info->uses_linear_center = TRUE;
255 break;
256 case TGSI_INTERPOLATE_LOC_CENTROID:
257 info->uses_linear_centroid = TRUE;
258 break;
259 case TGSI_INTERPOLATE_LOC_SAMPLE:
260 info->uses_linear_sample = TRUE;
261 break;
262 }
263 break;
264 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
265 }
266 }
267 }
268 }
269
270 /* check for indirect register reads */
271 if (src->Register.Indirect) {
272 info->indirect_files |= (1 << src->Register.File);
273 info->indirect_files_read |= (1 << src->Register.File);
274 }
275
276 /* Texture samplers */
277 if (src->Register.File == TGSI_FILE_SAMPLER) {
278 const unsigned index = src->Register.Index;
279
280 assert(fullinst->Instruction.Texture);
281 assert(index < ARRAY_SIZE(info->is_msaa_sampler));
282 assert(index < PIPE_MAX_SAMPLERS);
283
284 if (is_texture_inst(fullinst->Instruction.Opcode)) {
285 const unsigned target = fullinst->Texture.Texture;
286 assert(target < TGSI_TEXTURE_UNKNOWN);
287 /* for texture instructions, check that the texture instruction
288 * target matches the previous sampler view declaration (if there
289 * was one.)
290 */
291 if (info->sampler_targets[index] == TGSI_TEXTURE_UNKNOWN) {
292 /* probably no sampler view declaration */
293 info->sampler_targets[index] = target;
294 } else {
295 /* Make sure the texture instruction's sampler/target info
296 * agrees with the sampler view declaration.
297 */
298 assert(info->sampler_targets[index] == target);
299 }
300 /* MSAA samplers */
301 if (target == TGSI_TEXTURE_2D_MSAA ||
302 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
303 info->is_msaa_sampler[src->Register.Index] = TRUE;
304 }
305 }
306 }
307
308 if (is_memory_file(src->Register.File)) {
309 is_mem_inst = true;
310
311 if (tgsi_get_opcode_info(fullinst->Instruction.Opcode)->is_store) {
312 info->writes_memory = TRUE;
313
314 if (src->Register.File == TGSI_FILE_IMAGE &&
315 !src->Register.Indirect)
316 info->images_writemask |= 1 << src->Register.Index;
317 }
318 }
319 }
320
321 /* check for indirect register writes */
322 for (i = 0; i < fullinst->Instruction.NumDstRegs; i++) {
323 const struct tgsi_full_dst_register *dst = &fullinst->Dst[i];
324 if (dst->Register.Indirect) {
325 info->indirect_files |= (1 << dst->Register.File);
326 info->indirect_files_written |= (1 << dst->Register.File);
327 }
328
329 if (is_memory_file(dst->Register.File)) {
330 assert(fullinst->Instruction.Opcode == TGSI_OPCODE_STORE);
331
332 is_mem_inst = true;
333 info->writes_memory = TRUE;
334
335 if (dst->Register.File == TGSI_FILE_IMAGE &&
336 !dst->Register.Indirect)
337 info->images_writemask |= 1 << dst->Register.Index;
338 }
339 }
340
341 if (is_mem_inst)
342 info->num_memory_instructions++;
343
344 if (computes_derivative(fullinst->Instruction.Opcode))
345 info->uses_derivatives = true;
346
347 info->num_instructions++;
348 }
349
350
351 static void
352 scan_declaration(struct tgsi_shader_info *info,
353 const struct tgsi_full_declaration *fulldecl)
354 {
355 const uint file = fulldecl->Declaration.File;
356 const unsigned procType = info->processor;
357 uint reg;
358
359 if (fulldecl->Declaration.Array) {
360 unsigned array_id = fulldecl->Array.ArrayID;
361
362 switch (file) {
363 case TGSI_FILE_INPUT:
364 assert(array_id < ARRAY_SIZE(info->input_array_first));
365 info->input_array_first[array_id] = fulldecl->Range.First;
366 info->input_array_last[array_id] = fulldecl->Range.Last;
367 break;
368 case TGSI_FILE_OUTPUT:
369 assert(array_id < ARRAY_SIZE(info->output_array_first));
370 info->output_array_first[array_id] = fulldecl->Range.First;
371 info->output_array_last[array_id] = fulldecl->Range.Last;
372 break;
373 }
374 info->array_max[file] = MAX2(info->array_max[file], array_id);
375 }
376
377 for (reg = fulldecl->Range.First; reg <= fulldecl->Range.Last; reg++) {
378 unsigned semName = fulldecl->Semantic.Name;
379 unsigned semIndex = fulldecl->Semantic.Index +
380 (reg - fulldecl->Range.First);
381
382 /* only first 32 regs will appear in this bitfield */
383 info->file_mask[file] |= (1 << reg);
384 info->file_count[file]++;
385 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
386
387 if (file == TGSI_FILE_CONSTANT) {
388 int buffer = 0;
389
390 if (fulldecl->Declaration.Dimension)
391 buffer = fulldecl->Dim.Index2D;
392
393 info->const_file_max[buffer] =
394 MAX2(info->const_file_max[buffer], (int)reg);
395 }
396 else if (file == TGSI_FILE_INPUT) {
397 info->input_semantic_name[reg] = (ubyte) semName;
398 info->input_semantic_index[reg] = (ubyte) semIndex;
399 info->input_interpolate[reg] = (ubyte)fulldecl->Interp.Interpolate;
400 info->input_interpolate_loc[reg] = (ubyte)fulldecl->Interp.Location;
401 info->input_cylindrical_wrap[reg] = (ubyte)fulldecl->Interp.CylindricalWrap;
402
403 /* Vertex shaders can have inputs with holes between them. */
404 info->num_inputs = MAX2(info->num_inputs, reg + 1);
405
406 if (semName == TGSI_SEMANTIC_PRIMID)
407 info->uses_primid = TRUE;
408 else if (procType == PIPE_SHADER_FRAGMENT) {
409 if (semName == TGSI_SEMANTIC_POSITION)
410 info->reads_position = TRUE;
411 else if (semName == TGSI_SEMANTIC_FACE)
412 info->uses_frontface = TRUE;
413 }
414 }
415 else if (file == TGSI_FILE_SYSTEM_VALUE) {
416 unsigned index = fulldecl->Range.First;
417
418 info->system_value_semantic_name[index] = semName;
419 info->num_system_values = MAX2(info->num_system_values, index + 1);
420
421 switch (semName) {
422 case TGSI_SEMANTIC_INSTANCEID:
423 info->uses_instanceid = TRUE;
424 break;
425 case TGSI_SEMANTIC_VERTEXID:
426 info->uses_vertexid = TRUE;
427 break;
428 case TGSI_SEMANTIC_VERTEXID_NOBASE:
429 info->uses_vertexid_nobase = TRUE;
430 break;
431 case TGSI_SEMANTIC_BASEVERTEX:
432 info->uses_basevertex = TRUE;
433 break;
434 case TGSI_SEMANTIC_PRIMID:
435 info->uses_primid = TRUE;
436 break;
437 case TGSI_SEMANTIC_INVOCATIONID:
438 info->uses_invocationid = TRUE;
439 break;
440 case TGSI_SEMANTIC_POSITION:
441 info->reads_position = TRUE;
442 break;
443 case TGSI_SEMANTIC_FACE:
444 info->uses_frontface = TRUE;
445 break;
446 case TGSI_SEMANTIC_SAMPLEMASK:
447 info->reads_samplemask = TRUE;
448 break;
449 }
450 }
451 else if (file == TGSI_FILE_OUTPUT) {
452 info->output_semantic_name[reg] = (ubyte) semName;
453 info->output_semantic_index[reg] = (ubyte) semIndex;
454 info->num_outputs = MAX2(info->num_outputs, reg + 1);
455
456 if (semName == TGSI_SEMANTIC_COLOR)
457 info->colors_written |= 1 << semIndex;
458
459 if (procType == PIPE_SHADER_VERTEX ||
460 procType == PIPE_SHADER_GEOMETRY ||
461 procType == PIPE_SHADER_TESS_CTRL ||
462 procType == PIPE_SHADER_TESS_EVAL) {
463 switch (semName) {
464 case TGSI_SEMANTIC_VIEWPORT_INDEX:
465 info->writes_viewport_index = TRUE;
466 break;
467 case TGSI_SEMANTIC_LAYER:
468 info->writes_layer = TRUE;
469 break;
470 case TGSI_SEMANTIC_PSIZE:
471 info->writes_psize = TRUE;
472 break;
473 case TGSI_SEMANTIC_CLIPVERTEX:
474 info->writes_clipvertex = TRUE;
475 break;
476 }
477 }
478
479 if (procType == PIPE_SHADER_FRAGMENT) {
480 switch (semName) {
481 case TGSI_SEMANTIC_POSITION:
482 info->writes_z = TRUE;
483 break;
484 case TGSI_SEMANTIC_STENCIL:
485 info->writes_stencil = TRUE;
486 break;
487 case TGSI_SEMANTIC_SAMPLEMASK:
488 info->writes_samplemask = TRUE;
489 break;
490 }
491 }
492
493 if (procType == PIPE_SHADER_VERTEX) {
494 if (semName == TGSI_SEMANTIC_EDGEFLAG) {
495 info->writes_edgeflag = TRUE;
496 }
497 }
498 } else if (file == TGSI_FILE_SAMPLER) {
499 STATIC_ASSERT(sizeof(info->samplers_declared) * 8 >= PIPE_MAX_SAMPLERS);
500 info->samplers_declared |= 1u << reg;
501 } else if (file == TGSI_FILE_SAMPLER_VIEW) {
502 unsigned target = fulldecl->SamplerView.Resource;
503 unsigned type = fulldecl->SamplerView.ReturnTypeX;
504
505 assert(target < TGSI_TEXTURE_UNKNOWN);
506 if (info->sampler_targets[reg] == TGSI_TEXTURE_UNKNOWN) {
507 /* Save sampler target for this sampler index */
508 info->sampler_targets[reg] = target;
509 info->sampler_type[reg] = type;
510 } else {
511 /* if previously declared, make sure targets agree */
512 assert(info->sampler_targets[reg] == target);
513 assert(info->sampler_type[reg] == type);
514 }
515 } else if (file == TGSI_FILE_IMAGE) {
516 if (fulldecl->Image.Resource == TGSI_TEXTURE_BUFFER)
517 info->images_buffers |= 1 << reg;
518 }
519 }
520 }
521
522
523 static void
524 scan_immediate(struct tgsi_shader_info *info)
525 {
526 uint reg = info->immediate_count++;
527 uint file = TGSI_FILE_IMMEDIATE;
528
529 info->file_mask[file] |= (1 << reg);
530 info->file_count[file]++;
531 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
532 }
533
534
535 static void
536 scan_property(struct tgsi_shader_info *info,
537 const struct tgsi_full_property *fullprop)
538 {
539 unsigned name = fullprop->Property.PropertyName;
540 unsigned value = fullprop->u[0].Data;
541
542 assert(name < ARRAY_SIZE(info->properties));
543 info->properties[name] = value;
544
545 switch (name) {
546 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED:
547 info->num_written_clipdistance = value;
548 info->clipdist_writemask |= (1 << value) - 1;
549 break;
550 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED:
551 info->num_written_culldistance = value;
552 info->culldist_writemask |= (1 << value) - 1;
553 break;
554 }
555 }
556
557
558 /**
559 * Scan the given TGSI shader to collect information such as number of
560 * registers used, special instructions used, etc.
561 * \return info the result of the scan
562 */
563 void
564 tgsi_scan_shader(const struct tgsi_token *tokens,
565 struct tgsi_shader_info *info)
566 {
567 uint procType, i;
568 struct tgsi_parse_context parse;
569 unsigned current_depth = 0;
570
571 memset(info, 0, sizeof(*info));
572 for (i = 0; i < TGSI_FILE_COUNT; i++)
573 info->file_max[i] = -1;
574 for (i = 0; i < ARRAY_SIZE(info->const_file_max); i++)
575 info->const_file_max[i] = -1;
576 info->properties[TGSI_PROPERTY_GS_INVOCATIONS] = 1;
577 for (i = 0; i < ARRAY_SIZE(info->sampler_targets); i++)
578 info->sampler_targets[i] = TGSI_TEXTURE_UNKNOWN;
579
580 /**
581 ** Setup to begin parsing input shader
582 **/
583 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
584 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
585 return;
586 }
587 procType = parse.FullHeader.Processor.Processor;
588 assert(procType == PIPE_SHADER_FRAGMENT ||
589 procType == PIPE_SHADER_VERTEX ||
590 procType == PIPE_SHADER_GEOMETRY ||
591 procType == PIPE_SHADER_TESS_CTRL ||
592 procType == PIPE_SHADER_TESS_EVAL ||
593 procType == PIPE_SHADER_COMPUTE);
594 info->processor = procType;
595
596 /**
597 ** Loop over incoming program tokens/instructions
598 */
599 while (!tgsi_parse_end_of_tokens(&parse)) {
600 info->num_tokens++;
601
602 tgsi_parse_token( &parse );
603
604 switch( parse.FullToken.Token.Type ) {
605 case TGSI_TOKEN_TYPE_INSTRUCTION:
606 scan_instruction(info, &parse.FullToken.FullInstruction,
607 &current_depth);
608 break;
609 case TGSI_TOKEN_TYPE_DECLARATION:
610 scan_declaration(info, &parse.FullToken.FullDeclaration);
611 break;
612 case TGSI_TOKEN_TYPE_IMMEDIATE:
613 scan_immediate(info);
614 break;
615 case TGSI_TOKEN_TYPE_PROPERTY:
616 scan_property(info, &parse.FullToken.FullProperty);
617 break;
618 default:
619 assert(!"Unexpected TGSI token type");
620 }
621 }
622
623 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KILL_IF] ||
624 info->opcode_count[TGSI_OPCODE_KILL]);
625
626 /* The dimensions of the IN decleration in geometry shader have
627 * to be deduced from the type of the input primitive.
628 */
629 if (procType == PIPE_SHADER_GEOMETRY) {
630 unsigned input_primitive =
631 info->properties[TGSI_PROPERTY_GS_INPUT_PRIM];
632 int num_verts = u_vertices_per_prim(input_primitive);
633 int j;
634 info->file_count[TGSI_FILE_INPUT] = num_verts;
635 info->file_max[TGSI_FILE_INPUT] =
636 MAX2(info->file_max[TGSI_FILE_INPUT], num_verts - 1);
637 for (j = 0; j < num_verts; ++j) {
638 info->file_mask[TGSI_FILE_INPUT] |= (1 << j);
639 }
640 }
641
642 tgsi_parse_free(&parse);
643 }
644
645 /**
646 * Collect information about the arrays of a given register file.
647 *
648 * @param tokens TGSI shader
649 * @param file the register file to scan through
650 * @param max_array_id number of entries in @p arrays; should be equal to the
651 * highest array id, i.e. tgsi_shader_info::array_max[file].
652 * @param arrays info for array of each ID will be written to arrays[ID - 1].
653 */
654 void
655 tgsi_scan_arrays(const struct tgsi_token *tokens,
656 unsigned file,
657 unsigned max_array_id,
658 struct tgsi_array_info *arrays)
659 {
660 struct tgsi_parse_context parse;
661
662 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
663 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
664 return;
665 }
666
667 memset(arrays, 0, sizeof(arrays[0]) * max_array_id);
668
669 while (!tgsi_parse_end_of_tokens(&parse)) {
670 struct tgsi_full_instruction *inst;
671
672 tgsi_parse_token(&parse);
673
674 if (parse.FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
675 struct tgsi_full_declaration *decl = &parse.FullToken.FullDeclaration;
676
677 if (decl->Declaration.Array && decl->Declaration.File == file &&
678 decl->Array.ArrayID > 0 && decl->Array.ArrayID <= max_array_id) {
679 struct tgsi_array_info *array = &arrays[decl->Array.ArrayID - 1];
680 assert(!array->declared);
681 array->declared = true;
682 array->range = decl->Range;
683 }
684 }
685
686 if (parse.FullToken.Token.Type != TGSI_TOKEN_TYPE_INSTRUCTION)
687 continue;
688
689 inst = &parse.FullToken.FullInstruction;
690 for (unsigned i = 0; i < inst->Instruction.NumDstRegs; i++) {
691 const struct tgsi_full_dst_register *dst = &inst->Dst[i];
692 if (dst->Register.File != file)
693 continue;
694
695 if (dst->Register.Indirect) {
696 if (dst->Indirect.ArrayID > 0 &&
697 dst->Indirect.ArrayID <= max_array_id) {
698 arrays[dst->Indirect.ArrayID - 1].writemask |= dst->Register.WriteMask;
699 } else {
700 /* Indirect writes without an ArrayID can write anywhere. */
701 for (unsigned j = 0; j < max_array_id; ++j)
702 arrays[j].writemask |= dst->Register.WriteMask;
703 }
704 } else {
705 /* Check whether the write falls into any of the arrays anyway. */
706 for (unsigned j = 0; j < max_array_id; ++j) {
707 struct tgsi_array_info *array = &arrays[j];
708 if (array->declared &&
709 dst->Register.Index >= array->range.First &&
710 dst->Register.Index <= array->range.Last)
711 array->writemask |= dst->Register.WriteMask;
712 }
713 }
714 }
715 }
716
717 tgsi_parse_free(&parse);
718
719 return;
720 }
721
722
723 /**
724 * Check if the given shader is a "passthrough" shader consisting of only
725 * MOV instructions of the form: MOV OUT[n], IN[n]
726 *
727 */
728 boolean
729 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
730 {
731 struct tgsi_parse_context parse;
732
733 /**
734 ** Setup to begin parsing input shader
735 **/
736 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
737 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
738 return FALSE;
739 }
740
741 /**
742 ** Loop over incoming program tokens/instructions
743 */
744 while (!tgsi_parse_end_of_tokens(&parse)) {
745
746 tgsi_parse_token(&parse);
747
748 switch (parse.FullToken.Token.Type) {
749 case TGSI_TOKEN_TYPE_INSTRUCTION:
750 {
751 struct tgsi_full_instruction *fullinst =
752 &parse.FullToken.FullInstruction;
753 const struct tgsi_full_src_register *src =
754 &fullinst->Src[0];
755 const struct tgsi_full_dst_register *dst =
756 &fullinst->Dst[0];
757
758 /* Do a whole bunch of checks for a simple move */
759 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
760 (src->Register.File != TGSI_FILE_INPUT &&
761 src->Register.File != TGSI_FILE_SYSTEM_VALUE) ||
762 dst->Register.File != TGSI_FILE_OUTPUT ||
763 src->Register.Index != dst->Register.Index ||
764
765 src->Register.Negate ||
766 src->Register.Absolute ||
767
768 src->Register.SwizzleX != TGSI_SWIZZLE_X ||
769 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
770 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
771 src->Register.SwizzleW != TGSI_SWIZZLE_W ||
772
773 dst->Register.WriteMask != TGSI_WRITEMASK_XYZW)
774 {
775 tgsi_parse_free(&parse);
776 return FALSE;
777 }
778 }
779 break;
780
781 case TGSI_TOKEN_TYPE_DECLARATION:
782 /* fall-through */
783 case TGSI_TOKEN_TYPE_IMMEDIATE:
784 /* fall-through */
785 case TGSI_TOKEN_TYPE_PROPERTY:
786 /* fall-through */
787 default:
788 ; /* no-op */
789 }
790 }
791
792 tgsi_parse_free(&parse);
793
794 /* if we get here, it's a pass-through shader */
795 return TRUE;
796 }