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;
106 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
107 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
109 switch (fullinst
->Instruction
.Opcode
) {
111 case TGSI_OPCODE_UIF
:
112 case TGSI_OPCODE_BGNLOOP
:
114 info
->max_depth
= MAX2(info
->max_depth
, *current_depth
);
116 case TGSI_OPCODE_ENDIF
:
117 case TGSI_OPCODE_ENDLOOP
:
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];
130 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
131 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
133 input
= src0
->Register
.Index
;
135 /* For the INTERP opcodes, the interpolation is always
136 * PERSPECTIVE unless LINEAR is specified.
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
;
146 case TGSI_OPCODE_INTERP_OFFSET
:
147 info
->uses_persp_opcode_interp_offset
= TRUE
;
149 case TGSI_OPCODE_INTERP_SAMPLE
:
150 info
->uses_persp_opcode_interp_sample
= TRUE
;
155 case TGSI_INTERPOLATE_LINEAR
:
156 switch (fullinst
->Instruction
.Opcode
) {
157 case TGSI_OPCODE_INTERP_CENTROID
:
158 info
->uses_linear_opcode_interp_centroid
= TRUE
;
160 case TGSI_OPCODE_INTERP_OFFSET
:
161 info
->uses_linear_opcode_interp_offset
= TRUE
;
163 case TGSI_OPCODE_INTERP_SAMPLE
:
164 info
->uses_linear_opcode_interp_sample
= TRUE
;
171 if (fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
172 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
)
173 info
->uses_doubles
= TRUE
;
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
;
179 /* Mark which inputs are effectively used */
180 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
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
;
189 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
190 info
->input_usage_mask
[ind
] |= usage_mask
;
193 if (info
->processor
== PIPE_SHADER_FRAGMENT
&&
194 !src
->Register
.Indirect
) {
196 info
->input_semantic_name
[src
->Register
.Index
];
198 info
->input_semantic_index
[src
->Register
.Index
];
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
;
207 if (name
== TGSI_SEMANTIC_COLOR
) {
209 (1 << src
->Register
.SwizzleX
) |
210 (1 << src
->Register
.SwizzleY
) |
211 (1 << src
->Register
.SwizzleZ
) |
212 (1 << src
->Register
.SwizzleW
);
214 info
->colors_read
|= mask
<< (index
* 4);
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
);
225 /* Texture samplers */
226 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
227 const unsigned index
= src
->Register
.Index
;
229 assert(fullinst
->Instruction
.Texture
);
230 assert(index
< ARRAY_SIZE(info
->is_msaa_sampler
));
231 assert(index
< PIPE_MAX_SAMPLERS
);
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
240 if (info
->sampler_targets
[index
] == TGSI_TEXTURE_UNKNOWN
) {
241 /* probably no sampler view declaration */
242 info
->sampler_targets
[index
] = target
;
244 /* Make sure the texture instruction's sampler/target info
245 * agrees with the sampler view declaration.
247 assert(info
->sampler_targets
[index
] == target
);
250 if (target
== TGSI_TEXTURE_2D_MSAA
||
251 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
252 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
257 if (is_memory_file(src
->Register
.File
)) {
260 if (tgsi_get_opcode_info(fullinst
->Instruction
.Opcode
)->is_store
) {
261 info
->writes_memory
= TRUE
;
263 if (src
->Register
.File
== TGSI_FILE_IMAGE
&&
264 !src
->Register
.Indirect
)
265 info
->images_writemask
|= 1 << src
->Register
.Index
;
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
);
278 if (is_memory_file(dst
->Register
.File
)) {
279 assert(fullinst
->Instruction
.Opcode
== TGSI_OPCODE_STORE
);
282 info
->writes_memory
= TRUE
;
284 if (dst
->Register
.File
== TGSI_FILE_IMAGE
&&
285 !dst
->Register
.Indirect
)
286 info
->images_writemask
|= 1 << dst
->Register
.Index
;
291 info
->num_memory_instructions
++;
293 if (computes_derivative(fullinst
->Instruction
.Opcode
))
294 info
->uses_derivatives
= true;
296 info
->num_instructions
++;
301 scan_declaration(struct tgsi_shader_info
*info
,
302 const struct tgsi_full_declaration
*fulldecl
)
304 const uint file
= fulldecl
->Declaration
.File
;
305 const unsigned procType
= info
->processor
;
308 if (fulldecl
->Declaration
.Array
) {
309 unsigned array_id
= fulldecl
->Array
.ArrayID
;
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
;
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
;
323 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
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
);
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
);
336 if (file
== TGSI_FILE_CONSTANT
) {
339 if (fulldecl
->Declaration
.Dimension
)
340 buffer
= fulldecl
->Dim
.Index2D
;
342 info
->const_file_max
[buffer
] =
343 MAX2(info
->const_file_max
[buffer
], (int)reg
);
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
;
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);
357 assert(reg
< info
->num_inputs
);
360 /* Only interpolated varyings. Don't include POSITION.
361 * Don't include integer varyings, because they are not
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
;
377 case TGSI_INTERPOLATE_LOC_CENTROID
:
378 info
->uses_persp_centroid
= TRUE
;
380 case TGSI_INTERPOLATE_LOC_SAMPLE
:
381 info
->uses_persp_sample
= TRUE
;
385 case TGSI_INTERPOLATE_LINEAR
:
386 switch (fulldecl
->Interp
.Location
) {
387 case TGSI_INTERPOLATE_LOC_CENTER
:
388 info
->uses_linear_center
= TRUE
;
390 case TGSI_INTERPOLATE_LOC_CENTROID
:
391 info
->uses_linear_centroid
= TRUE
;
393 case TGSI_INTERPOLATE_LOC_SAMPLE
:
394 info
->uses_linear_sample
= TRUE
;
398 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
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
;
411 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
412 unsigned index
= fulldecl
->Range
.First
;
414 info
->system_value_semantic_name
[index
] = semName
;
415 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
418 case TGSI_SEMANTIC_INSTANCEID
:
419 info
->uses_instanceid
= TRUE
;
421 case TGSI_SEMANTIC_VERTEXID
:
422 info
->uses_vertexid
= TRUE
;
424 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
425 info
->uses_vertexid_nobase
= TRUE
;
427 case TGSI_SEMANTIC_BASEVERTEX
:
428 info
->uses_basevertex
= TRUE
;
430 case TGSI_SEMANTIC_PRIMID
:
431 info
->uses_primid
= TRUE
;
433 case TGSI_SEMANTIC_INVOCATIONID
:
434 info
->uses_invocationid
= TRUE
;
436 case TGSI_SEMANTIC_POSITION
:
437 info
->reads_position
= TRUE
;
439 case TGSI_SEMANTIC_FACE
:
440 info
->uses_frontface
= TRUE
;
442 case TGSI_SEMANTIC_SAMPLEMASK
:
443 info
->reads_samplemask
= TRUE
;
447 else if (file
== TGSI_FILE_OUTPUT
) {
448 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
449 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
451 assert(reg
< info
->num_outputs
);
453 if (semName
== TGSI_SEMANTIC_COLOR
)
454 info
->colors_written
|= 1 << semIndex
;
456 if (procType
== PIPE_SHADER_VERTEX
||
457 procType
== PIPE_SHADER_GEOMETRY
||
458 procType
== PIPE_SHADER_TESS_CTRL
||
459 procType
== PIPE_SHADER_TESS_EVAL
) {
461 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
462 info
->writes_viewport_index
= TRUE
;
464 case TGSI_SEMANTIC_LAYER
:
465 info
->writes_layer
= TRUE
;
467 case TGSI_SEMANTIC_PSIZE
:
468 info
->writes_psize
= TRUE
;
470 case TGSI_SEMANTIC_CLIPVERTEX
:
471 info
->writes_clipvertex
= TRUE
;
476 if (procType
== PIPE_SHADER_FRAGMENT
) {
478 case TGSI_SEMANTIC_POSITION
:
479 info
->writes_z
= TRUE
;
481 case TGSI_SEMANTIC_STENCIL
:
482 info
->writes_stencil
= TRUE
;
484 case TGSI_SEMANTIC_SAMPLEMASK
:
485 info
->writes_samplemask
= TRUE
;
490 if (procType
== PIPE_SHADER_VERTEX
) {
491 if (semName
== TGSI_SEMANTIC_EDGEFLAG
) {
492 info
->writes_edgeflag
= TRUE
;
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
;
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
;
508 /* if previously declared, make sure targets agree */
509 assert(info
->sampler_targets
[reg
] == target
);
510 assert(info
->sampler_type
[reg
] == type
);
512 } else if (file
== TGSI_FILE_IMAGE
) {
513 if (fulldecl
->Image
.Resource
== TGSI_TEXTURE_BUFFER
)
514 info
->images_buffers
|= 1 << reg
;
521 scan_immediate(struct tgsi_shader_info
*info
)
523 uint reg
= info
->immediate_count
++;
524 uint file
= TGSI_FILE_IMMEDIATE
;
526 info
->file_mask
[file
] |= (1 << reg
);
527 info
->file_count
[file
]++;
528 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
533 scan_property(struct tgsi_shader_info
*info
,
534 const struct tgsi_full_property
*fullprop
)
536 unsigned name
= fullprop
->Property
.PropertyName
;
537 unsigned value
= fullprop
->u
[0].Data
;
539 assert(name
< ARRAY_SIZE(info
->properties
));
540 info
->properties
[name
] = value
;
543 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
544 info
->num_written_clipdistance
= value
;
545 info
->clipdist_writemask
|= (1 << value
) - 1;
547 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
548 info
->num_written_culldistance
= value
;
549 info
->culldist_writemask
|= (1 << value
) - 1;
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
561 tgsi_scan_shader(const struct tgsi_token
*tokens
,
562 struct tgsi_shader_info
*info
)
565 struct tgsi_parse_context parse
;
566 unsigned current_depth
= 0;
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
;
578 ** Setup to begin parsing input shader
580 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
581 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
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
;
594 ** Loop over incoming program tokens/instructions
596 while (!tgsi_parse_end_of_tokens(&parse
)) {
599 tgsi_parse_token( &parse
);
601 switch( parse
.FullToken
.Token
.Type
) {
602 case TGSI_TOKEN_TYPE_INSTRUCTION
:
603 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
606 case TGSI_TOKEN_TYPE_DECLARATION
:
607 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
609 case TGSI_TOKEN_TYPE_IMMEDIATE
:
610 scan_immediate(info
);
612 case TGSI_TOKEN_TYPE_PROPERTY
:
613 scan_property(info
, &parse
.FullToken
.FullProperty
);
616 assert(!"Unexpected TGSI token type");
620 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
621 info
->opcode_count
[TGSI_OPCODE_KILL
]);
623 /* The dimensions of the IN decleration in geometry shader have
624 * to be deduced from the type of the input primitive.
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
);
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
);
639 tgsi_parse_free(&parse
);
643 * Collect information about the arrays of a given register file.
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].
652 tgsi_scan_arrays(const struct tgsi_token
*tokens
,
654 unsigned max_array_id
,
655 struct tgsi_array_info
*arrays
)
657 struct tgsi_parse_context parse
;
659 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
660 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
664 memset(arrays
, 0, sizeof(arrays
[0]) * max_array_id
);
666 while (!tgsi_parse_end_of_tokens(&parse
)) {
667 struct tgsi_full_instruction
*inst
;
669 tgsi_parse_token(&parse
);
671 if (parse
.FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
672 struct tgsi_full_declaration
*decl
= &parse
.FullToken
.FullDeclaration
;
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
;
683 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
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
)
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
;
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
;
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
;
714 tgsi_parse_free(&parse
);
721 * Check if the given shader is a "passthrough" shader consisting of only
722 * MOV instructions of the form: MOV OUT[n], IN[n]
726 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
728 struct tgsi_parse_context parse
;
731 ** Setup to begin parsing input shader
733 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
734 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
739 ** Loop over incoming program tokens/instructions
741 while (!tgsi_parse_end_of_tokens(&parse
)) {
743 tgsi_parse_token(&parse
);
745 switch (parse
.FullToken
.Token
.Type
) {
746 case TGSI_TOKEN_TYPE_INSTRUCTION
:
748 struct tgsi_full_instruction
*fullinst
=
749 &parse
.FullToken
.FullInstruction
;
750 const struct tgsi_full_src_register
*src
=
752 const struct tgsi_full_dst_register
*dst
=
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
||
762 src
->Register
.Negate
||
763 src
->Register
.Absolute
||
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
||
770 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
772 tgsi_parse_free(&parse
);
778 case TGSI_TOKEN_TYPE_DECLARATION
:
780 case TGSI_TOKEN_TYPE_IMMEDIATE
:
782 case TGSI_TOKEN_TYPE_PROPERTY
:
789 tgsi_parse_free(&parse
);
791 /* if we get here, it's a pass-through shader */