1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
5 * Copyright 2008 VMware, Inc. All rights Reserved.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
30 * TGSI program scan utility.
31 * Used to determine which registers and instructions are used by a shader.
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"
48 is_memory_file(unsigned file
)
50 return file
== TGSI_FILE_SAMPLER
||
51 file
== TGSI_FILE_SAMPLER_VIEW
||
52 file
== TGSI_FILE_IMAGE
||
53 file
== TGSI_FILE_BUFFER
;
58 * Is the opcode a "true" texture instruction which samples from a
62 is_texture_inst(unsigned opcode
)
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
);
73 * Is the opcode an instruction which computes a derivative explicitly or
77 computes_derivative(unsigned opcode
)
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
;
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
;
99 scan_instruction(struct tgsi_shader_info
*info
,
100 const struct tgsi_full_instruction
*fullinst
,
101 unsigned *current_depth
)
104 bool is_mem_inst
= false;
105 bool is_interp_instruction
= false;
107 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
108 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
110 switch (fullinst
->Instruction
.Opcode
) {
112 case TGSI_OPCODE_UIF
:
113 case TGSI_OPCODE_BGNLOOP
:
115 info
->max_depth
= MAX2(info
->max_depth
, *current_depth
);
117 case TGSI_OPCODE_ENDIF
:
118 case TGSI_OPCODE_ENDLOOP
:
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];
131 is_interp_instruction
= true;
133 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
134 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
136 input
= src0
->Register
.Index
;
138 /* For the INTERP opcodes, the interpolation is always
139 * PERSPECTIVE unless LINEAR is specified.
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
;
149 case TGSI_OPCODE_INTERP_OFFSET
:
150 info
->uses_persp_opcode_interp_offset
= TRUE
;
152 case TGSI_OPCODE_INTERP_SAMPLE
:
153 info
->uses_persp_opcode_interp_sample
= TRUE
;
158 case TGSI_INTERPOLATE_LINEAR
:
159 switch (fullinst
->Instruction
.Opcode
) {
160 case TGSI_OPCODE_INTERP_CENTROID
:
161 info
->uses_linear_opcode_interp_centroid
= TRUE
;
163 case TGSI_OPCODE_INTERP_OFFSET
:
164 info
->uses_linear_opcode_interp_offset
= TRUE
;
166 case TGSI_OPCODE_INTERP_SAMPLE
:
167 info
->uses_linear_opcode_interp_sample
= TRUE
;
174 if (fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
175 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
)
176 info
->uses_doubles
= TRUE
;
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
;
182 /* Mark which inputs are effectively used */
183 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
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
;
192 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
193 info
->input_usage_mask
[ind
] |= usage_mask
;
196 if (info
->processor
== PIPE_SHADER_FRAGMENT
) {
197 unsigned name
, index
, input
;
199 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
200 input
= info
->input_array_first
[src
->Indirect
.ArrayID
];
202 input
= src
->Register
.Index
;
204 name
= info
->input_semantic_name
[input
];
205 index
= info
->input_semantic_index
[input
];
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
;
214 if (name
== TGSI_SEMANTIC_COLOR
) {
216 (1 << src
->Register
.SwizzleX
) |
217 (1 << src
->Register
.SwizzleY
) |
218 (1 << src
->Register
.SwizzleZ
) |
219 (1 << src
->Register
.SwizzleW
);
221 info
->colors_read
|= mask
<< (index
* 4);
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.
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
;
243 case TGSI_INTERPOLATE_LOC_CENTROID
:
244 info
->uses_persp_centroid
= TRUE
;
246 case TGSI_INTERPOLATE_LOC_SAMPLE
:
247 info
->uses_persp_sample
= TRUE
;
251 case TGSI_INTERPOLATE_LINEAR
:
252 switch (info
->input_interpolate_loc
[input
]) {
253 case TGSI_INTERPOLATE_LOC_CENTER
:
254 info
->uses_linear_center
= TRUE
;
256 case TGSI_INTERPOLATE_LOC_CENTROID
:
257 info
->uses_linear_centroid
= TRUE
;
259 case TGSI_INTERPOLATE_LOC_SAMPLE
:
260 info
->uses_linear_sample
= TRUE
;
264 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
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
);
276 /* Texture samplers */
277 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
278 const unsigned index
= src
->Register
.Index
;
280 assert(fullinst
->Instruction
.Texture
);
281 assert(index
< ARRAY_SIZE(info
->is_msaa_sampler
));
282 assert(index
< PIPE_MAX_SAMPLERS
);
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
291 if (info
->sampler_targets
[index
] == TGSI_TEXTURE_UNKNOWN
) {
292 /* probably no sampler view declaration */
293 info
->sampler_targets
[index
] = target
;
295 /* Make sure the texture instruction's sampler/target info
296 * agrees with the sampler view declaration.
298 assert(info
->sampler_targets
[index
] == target
);
301 if (target
== TGSI_TEXTURE_2D_MSAA
||
302 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
303 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
308 if (is_memory_file(src
->Register
.File
)) {
311 if (tgsi_get_opcode_info(fullinst
->Instruction
.Opcode
)->is_store
) {
312 info
->writes_memory
= TRUE
;
314 if (src
->Register
.File
== TGSI_FILE_IMAGE
&&
315 !src
->Register
.Indirect
)
316 info
->images_writemask
|= 1 << src
->Register
.Index
;
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
);
329 if (is_memory_file(dst
->Register
.File
)) {
330 assert(fullinst
->Instruction
.Opcode
== TGSI_OPCODE_STORE
);
333 info
->writes_memory
= TRUE
;
335 if (dst
->Register
.File
== TGSI_FILE_IMAGE
&&
336 !dst
->Register
.Indirect
)
337 info
->images_writemask
|= 1 << dst
->Register
.Index
;
342 info
->num_memory_instructions
++;
344 if (computes_derivative(fullinst
->Instruction
.Opcode
))
345 info
->uses_derivatives
= true;
347 info
->num_instructions
++;
352 scan_declaration(struct tgsi_shader_info
*info
,
353 const struct tgsi_full_declaration
*fulldecl
)
355 const uint file
= fulldecl
->Declaration
.File
;
356 const unsigned procType
= info
->processor
;
359 if (fulldecl
->Declaration
.Array
) {
360 unsigned array_id
= fulldecl
->Array
.ArrayID
;
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
;
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
;
374 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
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
);
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
);
387 if (file
== TGSI_FILE_CONSTANT
) {
390 if (fulldecl
->Declaration
.Dimension
)
391 buffer
= fulldecl
->Dim
.Index2D
;
393 info
->const_file_max
[buffer
] =
394 MAX2(info
->const_file_max
[buffer
], (int)reg
);
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
;
403 /* Vertex shaders can have inputs with holes between them. */
404 info
->num_inputs
= MAX2(info
->num_inputs
, reg
+ 1);
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
;
415 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
416 unsigned index
= fulldecl
->Range
.First
;
418 info
->system_value_semantic_name
[index
] = semName
;
419 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
422 case TGSI_SEMANTIC_INSTANCEID
:
423 info
->uses_instanceid
= TRUE
;
425 case TGSI_SEMANTIC_VERTEXID
:
426 info
->uses_vertexid
= TRUE
;
428 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
429 info
->uses_vertexid_nobase
= TRUE
;
431 case TGSI_SEMANTIC_BASEVERTEX
:
432 info
->uses_basevertex
= TRUE
;
434 case TGSI_SEMANTIC_PRIMID
:
435 info
->uses_primid
= TRUE
;
437 case TGSI_SEMANTIC_INVOCATIONID
:
438 info
->uses_invocationid
= TRUE
;
440 case TGSI_SEMANTIC_POSITION
:
441 info
->reads_position
= TRUE
;
443 case TGSI_SEMANTIC_FACE
:
444 info
->uses_frontface
= TRUE
;
446 case TGSI_SEMANTIC_SAMPLEMASK
:
447 info
->reads_samplemask
= TRUE
;
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);
456 if (semName
== TGSI_SEMANTIC_COLOR
)
457 info
->colors_written
|= 1 << semIndex
;
459 if (procType
== PIPE_SHADER_VERTEX
||
460 procType
== PIPE_SHADER_GEOMETRY
||
461 procType
== PIPE_SHADER_TESS_CTRL
||
462 procType
== PIPE_SHADER_TESS_EVAL
) {
464 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
465 info
->writes_viewport_index
= TRUE
;
467 case TGSI_SEMANTIC_LAYER
:
468 info
->writes_layer
= TRUE
;
470 case TGSI_SEMANTIC_PSIZE
:
471 info
->writes_psize
= TRUE
;
473 case TGSI_SEMANTIC_CLIPVERTEX
:
474 info
->writes_clipvertex
= TRUE
;
479 if (procType
== PIPE_SHADER_FRAGMENT
) {
481 case TGSI_SEMANTIC_POSITION
:
482 info
->writes_z
= TRUE
;
484 case TGSI_SEMANTIC_STENCIL
:
485 info
->writes_stencil
= TRUE
;
487 case TGSI_SEMANTIC_SAMPLEMASK
:
488 info
->writes_samplemask
= TRUE
;
493 if (procType
== PIPE_SHADER_VERTEX
) {
494 if (semName
== TGSI_SEMANTIC_EDGEFLAG
) {
495 info
->writes_edgeflag
= TRUE
;
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
;
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
;
511 /* if previously declared, make sure targets agree */
512 assert(info
->sampler_targets
[reg
] == target
);
513 assert(info
->sampler_type
[reg
] == type
);
515 } else if (file
== TGSI_FILE_IMAGE
) {
516 if (fulldecl
->Image
.Resource
== TGSI_TEXTURE_BUFFER
)
517 info
->images_buffers
|= 1 << reg
;
524 scan_immediate(struct tgsi_shader_info
*info
)
526 uint reg
= info
->immediate_count
++;
527 uint file
= TGSI_FILE_IMMEDIATE
;
529 info
->file_mask
[file
] |= (1 << reg
);
530 info
->file_count
[file
]++;
531 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
536 scan_property(struct tgsi_shader_info
*info
,
537 const struct tgsi_full_property
*fullprop
)
539 unsigned name
= fullprop
->Property
.PropertyName
;
540 unsigned value
= fullprop
->u
[0].Data
;
542 assert(name
< ARRAY_SIZE(info
->properties
));
543 info
->properties
[name
] = value
;
546 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
547 info
->num_written_clipdistance
= value
;
548 info
->clipdist_writemask
|= (1 << value
) - 1;
550 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
551 info
->num_written_culldistance
= value
;
552 info
->culldist_writemask
|= (1 << value
) - 1;
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
564 tgsi_scan_shader(const struct tgsi_token
*tokens
,
565 struct tgsi_shader_info
*info
)
568 struct tgsi_parse_context parse
;
569 unsigned current_depth
= 0;
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
;
581 ** Setup to begin parsing input shader
583 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
584 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
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
;
597 ** Loop over incoming program tokens/instructions
599 while (!tgsi_parse_end_of_tokens(&parse
)) {
602 tgsi_parse_token( &parse
);
604 switch( parse
.FullToken
.Token
.Type
) {
605 case TGSI_TOKEN_TYPE_INSTRUCTION
:
606 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
609 case TGSI_TOKEN_TYPE_DECLARATION
:
610 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
612 case TGSI_TOKEN_TYPE_IMMEDIATE
:
613 scan_immediate(info
);
615 case TGSI_TOKEN_TYPE_PROPERTY
:
616 scan_property(info
, &parse
.FullToken
.FullProperty
);
619 assert(!"Unexpected TGSI token type");
623 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
624 info
->opcode_count
[TGSI_OPCODE_KILL
]);
626 /* The dimensions of the IN decleration in geometry shader have
627 * to be deduced from the type of the input primitive.
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
);
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
);
642 tgsi_parse_free(&parse
);
646 * Collect information about the arrays of a given register file.
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].
655 tgsi_scan_arrays(const struct tgsi_token
*tokens
,
657 unsigned max_array_id
,
658 struct tgsi_array_info
*arrays
)
660 struct tgsi_parse_context parse
;
662 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
663 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
667 memset(arrays
, 0, sizeof(arrays
[0]) * max_array_id
);
669 while (!tgsi_parse_end_of_tokens(&parse
)) {
670 struct tgsi_full_instruction
*inst
;
672 tgsi_parse_token(&parse
);
674 if (parse
.FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
675 struct tgsi_full_declaration
*decl
= &parse
.FullToken
.FullDeclaration
;
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
;
686 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
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
)
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
;
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
;
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
;
717 tgsi_parse_free(&parse
);
724 * Check if the given shader is a "passthrough" shader consisting of only
725 * MOV instructions of the form: MOV OUT[n], IN[n]
729 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
731 struct tgsi_parse_context parse
;
734 ** Setup to begin parsing input shader
736 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
737 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
742 ** Loop over incoming program tokens/instructions
744 while (!tgsi_parse_end_of_tokens(&parse
)) {
746 tgsi_parse_token(&parse
);
748 switch (parse
.FullToken
.Token
.Type
) {
749 case TGSI_TOKEN_TYPE_INSTRUCTION
:
751 struct tgsi_full_instruction
*fullinst
=
752 &parse
.FullToken
.FullInstruction
;
753 const struct tgsi_full_src_register
*src
=
755 const struct tgsi_full_dst_register
*dst
=
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
||
765 src
->Register
.Negate
||
766 src
->Register
.Absolute
||
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
||
773 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
775 tgsi_parse_free(&parse
);
781 case TGSI_TOKEN_TYPE_DECLARATION
:
783 case TGSI_TOKEN_TYPE_IMMEDIATE
:
785 case TGSI_TOKEN_TYPE_PROPERTY
:
792 tgsi_parse_free(&parse
);
794 /* if we get here, it's a pass-through shader */