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