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_mem_query_inst(unsigned opcode
)
60 return opcode
== TGSI_OPCODE_RESQ
||
61 opcode
== TGSI_OPCODE_TXQ
||
62 opcode
== TGSI_OPCODE_TXQS
||
63 opcode
== TGSI_OPCODE_TXQ_LZ
||
64 opcode
== TGSI_OPCODE_LODQ
;
68 * Is the opcode a "true" texture instruction which samples from a
72 is_texture_inst(unsigned opcode
)
74 return (!is_mem_query_inst(opcode
) &&
75 tgsi_get_opcode_info(opcode
)->is_tex
);
80 * Is the opcode an instruction which computes a derivative explicitly or
84 computes_derivative(unsigned opcode
)
86 if (tgsi_get_opcode_info(opcode
)->is_tex
) {
87 return opcode
!= TGSI_OPCODE_TG4
&&
88 opcode
!= TGSI_OPCODE_TXD
&&
89 opcode
!= TGSI_OPCODE_TXF
&&
90 opcode
!= TGSI_OPCODE_TXF_LZ
&&
91 opcode
!= TGSI_OPCODE_TEX_LZ
&&
92 opcode
!= TGSI_OPCODE_TXL
&&
93 opcode
!= TGSI_OPCODE_TXL2
&&
94 opcode
!= TGSI_OPCODE_TXQ
&&
95 opcode
!= TGSI_OPCODE_TXQ_LZ
&&
96 opcode
!= TGSI_OPCODE_TXQS
;
99 return opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
||
100 opcode
== TGSI_OPCODE_DDY
|| opcode
== TGSI_OPCODE_DDY_FINE
||
101 opcode
== TGSI_OPCODE_SAMPLE
||
102 opcode
== TGSI_OPCODE_SAMPLE_B
||
103 opcode
== TGSI_OPCODE_SAMPLE_C
;
108 scan_src_operand(struct tgsi_shader_info
*info
,
109 const struct tgsi_full_instruction
*fullinst
,
110 const struct tgsi_full_src_register
*src
,
113 bool is_interp_instruction
,
116 int ind
= src
->Register
.Index
;
118 if (info
->processor
== PIPE_SHADER_COMPUTE
&&
119 src
->Register
.File
== TGSI_FILE_SYSTEM_VALUE
) {
120 unsigned swizzle
[4], i
, name
;
122 name
= info
->system_value_semantic_name
[src
->Register
.Index
];
123 swizzle
[0] = src
->Register
.SwizzleX
;
124 swizzle
[1] = src
->Register
.SwizzleY
;
125 swizzle
[2] = src
->Register
.SwizzleZ
;
126 swizzle
[3] = src
->Register
.SwizzleW
;
129 case TGSI_SEMANTIC_THREAD_ID
:
130 case TGSI_SEMANTIC_BLOCK_ID
:
131 for (i
= 0; i
< 4; i
++) {
132 if (swizzle
[i
] <= TGSI_SWIZZLE_Z
) {
133 if (name
== TGSI_SEMANTIC_THREAD_ID
)
134 info
->uses_thread_id
[swizzle
[i
]] = true;
136 info
->uses_block_id
[swizzle
[i
]] = true;
140 case TGSI_SEMANTIC_BLOCK_SIZE
:
141 /* The block size is translated to IMM with a fixed block size. */
142 if (info
->properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0)
143 info
->uses_block_size
= true;
145 case TGSI_SEMANTIC_GRID_SIZE
:
146 info
->uses_grid_size
= true;
151 /* Mark which inputs are effectively used */
152 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
153 if (src
->Register
.Indirect
) {
154 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
155 info
->input_usage_mask
[ind
] |= usage_mask
;
159 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
160 info
->input_usage_mask
[ind
] |= usage_mask
;
163 if (info
->processor
== PIPE_SHADER_FRAGMENT
) {
164 unsigned name
, index
, input
;
166 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
167 input
= info
->input_array_first
[src
->Indirect
.ArrayID
];
169 input
= src
->Register
.Index
;
171 name
= info
->input_semantic_name
[input
];
172 index
= info
->input_semantic_index
[input
];
174 if (name
== TGSI_SEMANTIC_POSITION
&&
175 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
176 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
177 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
178 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
))
179 info
->reads_z
= TRUE
;
181 if (name
== TGSI_SEMANTIC_COLOR
) {
183 (1 << src
->Register
.SwizzleX
) |
184 (1 << src
->Register
.SwizzleY
) |
185 (1 << src
->Register
.SwizzleZ
) |
186 (1 << src
->Register
.SwizzleW
);
188 info
->colors_read
|= mask
<< (index
* 4);
191 /* Process only interpolated varyings. Don't include POSITION.
192 * Don't include integer varyings, because they are not
193 * interpolated. Don't process inputs interpolated by INTERP
194 * opcodes. Those are tracked separately.
196 if ((!is_interp_instruction
|| src_index
!= 0) &&
197 (name
== TGSI_SEMANTIC_GENERIC
||
198 name
== TGSI_SEMANTIC_TEXCOORD
||
199 name
== TGSI_SEMANTIC_COLOR
||
200 name
== TGSI_SEMANTIC_BCOLOR
||
201 name
== TGSI_SEMANTIC_FOG
||
202 name
== TGSI_SEMANTIC_CLIPDIST
)) {
203 switch (info
->input_interpolate
[input
]) {
204 case TGSI_INTERPOLATE_COLOR
:
205 case TGSI_INTERPOLATE_PERSPECTIVE
:
206 switch (info
->input_interpolate_loc
[input
]) {
207 case TGSI_INTERPOLATE_LOC_CENTER
:
208 info
->uses_persp_center
= TRUE
;
210 case TGSI_INTERPOLATE_LOC_CENTROID
:
211 info
->uses_persp_centroid
= TRUE
;
213 case TGSI_INTERPOLATE_LOC_SAMPLE
:
214 info
->uses_persp_sample
= TRUE
;
218 case TGSI_INTERPOLATE_LINEAR
:
219 switch (info
->input_interpolate_loc
[input
]) {
220 case TGSI_INTERPOLATE_LOC_CENTER
:
221 info
->uses_linear_center
= TRUE
;
223 case TGSI_INTERPOLATE_LOC_CENTROID
:
224 info
->uses_linear_centroid
= TRUE
;
226 case TGSI_INTERPOLATE_LOC_SAMPLE
:
227 info
->uses_linear_sample
= TRUE
;
231 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
237 if (info
->processor
== PIPE_SHADER_TESS_CTRL
&&
238 src
->Register
.File
== TGSI_FILE_OUTPUT
) {
241 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
242 input
= info
->output_array_first
[src
->Indirect
.ArrayID
];
244 input
= src
->Register
.Index
;
246 switch (info
->output_semantic_name
[input
]) {
247 case TGSI_SEMANTIC_PATCH
:
248 info
->reads_perpatch_outputs
= true;
250 case TGSI_SEMANTIC_TESSINNER
:
251 case TGSI_SEMANTIC_TESSOUTER
:
252 info
->reads_tessfactor_outputs
= true;
255 info
->reads_pervertex_outputs
= true;
259 /* check for indirect register reads */
260 if (src
->Register
.Indirect
) {
261 info
->indirect_files
|= (1 << src
->Register
.File
);
262 info
->indirect_files_read
|= (1 << src
->Register
.File
);
264 /* record indirect constant buffer indexing */
265 if (src
->Register
.File
== TGSI_FILE_CONSTANT
) {
266 if (src
->Register
.Dimension
) {
267 if (src
->Dimension
.Indirect
)
268 info
->const_buffers_indirect
= info
->const_buffers_declared
;
270 info
->const_buffers_indirect
|= 1u << src
->Dimension
.Index
;
272 info
->const_buffers_indirect
|= 1;
277 if (src
->Register
.Dimension
&& src
->Dimension
.Indirect
)
278 info
->dim_indirect_files
|= 1u << src
->Register
.File
;
280 /* Texture samplers */
281 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
282 const unsigned index
= src
->Register
.Index
;
284 assert(fullinst
->Instruction
.Texture
);
285 assert(index
< ARRAY_SIZE(info
->is_msaa_sampler
));
286 assert(index
< PIPE_MAX_SAMPLERS
);
288 if (is_texture_inst(fullinst
->Instruction
.Opcode
)) {
289 const unsigned target
= fullinst
->Texture
.Texture
;
290 assert(target
< TGSI_TEXTURE_UNKNOWN
);
291 /* for texture instructions, check that the texture instruction
292 * target matches the previous sampler view declaration (if there
295 if (info
->sampler_targets
[index
] == TGSI_TEXTURE_UNKNOWN
) {
296 /* probably no sampler view declaration */
297 info
->sampler_targets
[index
] = target
;
299 /* Make sure the texture instruction's sampler/target info
300 * agrees with the sampler view declaration.
302 assert(info
->sampler_targets
[index
] == target
);
305 if (target
== TGSI_TEXTURE_2D_MSAA
||
306 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
307 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
312 if (is_memory_file(src
->Register
.File
) &&
313 !is_mem_query_inst(fullinst
->Instruction
.Opcode
)) {
316 if (tgsi_get_opcode_info(fullinst
->Instruction
.Opcode
)->is_store
) {
317 info
->writes_memory
= TRUE
;
319 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
320 if (src
->Register
.Indirect
)
321 info
->images_atomic
= info
->images_declared
;
323 info
->images_atomic
|= 1 << src
->Register
.Index
;
324 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
325 if (src
->Register
.Indirect
)
326 info
->shader_buffers_atomic
= info
->shader_buffers_declared
;
328 info
->shader_buffers_atomic
|= 1 << src
->Register
.Index
;
331 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
332 if (src
->Register
.Indirect
)
333 info
->images_load
= info
->images_declared
;
335 info
->images_load
|= 1 << src
->Register
.Index
;
336 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
337 if (src
->Register
.Indirect
)
338 info
->shader_buffers_load
= info
->shader_buffers_declared
;
340 info
->shader_buffers_load
|= 1 << src
->Register
.Index
;
348 scan_instruction(struct tgsi_shader_info
*info
,
349 const struct tgsi_full_instruction
*fullinst
,
350 unsigned *current_depth
)
353 bool is_mem_inst
= false;
354 bool is_interp_instruction
= false;
356 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
357 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
359 switch (fullinst
->Instruction
.Opcode
) {
361 case TGSI_OPCODE_UIF
:
362 case TGSI_OPCODE_BGNLOOP
:
364 info
->max_depth
= MAX2(info
->max_depth
, *current_depth
);
366 case TGSI_OPCODE_ENDIF
:
367 case TGSI_OPCODE_ENDLOOP
:
374 if (fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_CENTROID
||
375 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
376 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
377 const struct tgsi_full_src_register
*src0
= &fullinst
->Src
[0];
380 is_interp_instruction
= true;
382 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
383 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
385 input
= src0
->Register
.Index
;
387 /* For the INTERP opcodes, the interpolation is always
388 * PERSPECTIVE unless LINEAR is specified.
390 switch (info
->input_interpolate
[input
]) {
391 case TGSI_INTERPOLATE_COLOR
:
392 case TGSI_INTERPOLATE_CONSTANT
:
393 case TGSI_INTERPOLATE_PERSPECTIVE
:
394 switch (fullinst
->Instruction
.Opcode
) {
395 case TGSI_OPCODE_INTERP_CENTROID
:
396 info
->uses_persp_opcode_interp_centroid
= TRUE
;
398 case TGSI_OPCODE_INTERP_OFFSET
:
399 info
->uses_persp_opcode_interp_offset
= TRUE
;
401 case TGSI_OPCODE_INTERP_SAMPLE
:
402 info
->uses_persp_opcode_interp_sample
= TRUE
;
407 case TGSI_INTERPOLATE_LINEAR
:
408 switch (fullinst
->Instruction
.Opcode
) {
409 case TGSI_OPCODE_INTERP_CENTROID
:
410 info
->uses_linear_opcode_interp_centroid
= TRUE
;
412 case TGSI_OPCODE_INTERP_OFFSET
:
413 info
->uses_linear_opcode_interp_offset
= TRUE
;
415 case TGSI_OPCODE_INTERP_SAMPLE
:
416 info
->uses_linear_opcode_interp_sample
= TRUE
;
423 if (fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
424 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
)
425 info
->uses_doubles
= TRUE
;
427 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
428 scan_src_operand(info
, fullinst
, &fullinst
->Src
[i
], i
,
429 tgsi_util_get_inst_usage_mask(fullinst
, i
),
430 is_interp_instruction
, &is_mem_inst
);
433 if (fullinst
->Instruction
.Texture
) {
434 for (i
= 0; i
< fullinst
->Texture
.NumOffsets
; i
++) {
435 struct tgsi_full_src_register src
= {{0}};
437 src
.Register
.File
= fullinst
->TexOffsets
[i
].File
;
438 src
.Register
.Index
= fullinst
->TexOffsets
[i
].Index
;
439 src
.Register
.SwizzleX
= fullinst
->TexOffsets
[i
].SwizzleX
;
440 src
.Register
.SwizzleY
= fullinst
->TexOffsets
[i
].SwizzleY
;
441 src
.Register
.SwizzleZ
= fullinst
->TexOffsets
[i
].SwizzleZ
;
443 /* The usage mask is suboptimal but should be safe. */
444 scan_src_operand(info
, fullinst
, &src
, 0, TGSI_WRITEMASK_XYZ
,
445 false, &is_mem_inst
);
449 /* check for indirect register writes */
450 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
451 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
452 if (dst
->Register
.Indirect
) {
453 info
->indirect_files
|= (1 << dst
->Register
.File
);
454 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
457 if (dst
->Register
.Dimension
&& dst
->Dimension
.Indirect
)
458 info
->dim_indirect_files
|= 1u << dst
->Register
.File
;
460 if (is_memory_file(dst
->Register
.File
)) {
461 assert(fullinst
->Instruction
.Opcode
== TGSI_OPCODE_STORE
);
464 info
->writes_memory
= TRUE
;
466 if (dst
->Register
.File
== TGSI_FILE_IMAGE
) {
467 if (dst
->Register
.Indirect
)
468 info
->images_store
= info
->images_declared
;
470 info
->images_store
|= 1 << dst
->Register
.Index
;
471 } else if (dst
->Register
.File
== TGSI_FILE_BUFFER
) {
472 if (dst
->Register
.Indirect
)
473 info
->shader_buffers_store
= info
->shader_buffers_declared
;
475 info
->shader_buffers_store
|= 1 << dst
->Register
.Index
;
481 info
->num_memory_instructions
++;
483 if (computes_derivative(fullinst
->Instruction
.Opcode
))
484 info
->uses_derivatives
= true;
486 info
->num_instructions
++;
491 scan_declaration(struct tgsi_shader_info
*info
,
492 const struct tgsi_full_declaration
*fulldecl
)
494 const uint file
= fulldecl
->Declaration
.File
;
495 const unsigned procType
= info
->processor
;
498 if (fulldecl
->Declaration
.Array
) {
499 unsigned array_id
= fulldecl
->Array
.ArrayID
;
502 case TGSI_FILE_INPUT
:
503 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
504 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
505 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
507 case TGSI_FILE_OUTPUT
:
508 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
509 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
510 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
513 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
516 for (reg
= fulldecl
->Range
.First
; reg
<= fulldecl
->Range
.Last
; reg
++) {
517 unsigned semName
= fulldecl
->Semantic
.Name
;
518 unsigned semIndex
= fulldecl
->Semantic
.Index
+
519 (reg
- fulldecl
->Range
.First
);
521 unsigned index
, target
, type
;
523 /* only first 32 regs will appear in this bitfield */
524 info
->file_mask
[file
] |= (1 << reg
);
525 info
->file_count
[file
]++;
526 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
529 case TGSI_FILE_CONSTANT
:
532 if (fulldecl
->Declaration
.Dimension
)
533 buffer
= fulldecl
->Dim
.Index2D
;
535 info
->const_file_max
[buffer
] =
536 MAX2(info
->const_file_max
[buffer
], (int)reg
);
537 info
->const_buffers_declared
|= 1u << buffer
;
540 case TGSI_FILE_IMAGE
:
541 info
->images_declared
|= 1u << reg
;
542 if (fulldecl
->Image
.Resource
== TGSI_TEXTURE_BUFFER
)
543 info
->images_buffers
|= 1 << reg
;
546 case TGSI_FILE_BUFFER
:
547 info
->shader_buffers_declared
|= 1u << reg
;
550 case TGSI_FILE_INPUT
:
551 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
552 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
553 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
554 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
555 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
557 /* Vertex shaders can have inputs with holes between them. */
558 info
->num_inputs
= MAX2(info
->num_inputs
, reg
+ 1);
561 case TGSI_SEMANTIC_PRIMID
:
562 info
->uses_primid
= true;
564 case TGSI_SEMANTIC_POSITION
:
565 info
->reads_position
= true;
567 case TGSI_SEMANTIC_FACE
:
568 info
->uses_frontface
= true;
573 case TGSI_FILE_SYSTEM_VALUE
:
574 index
= fulldecl
->Range
.First
;
576 info
->system_value_semantic_name
[index
] = semName
;
577 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
580 case TGSI_SEMANTIC_INSTANCEID
:
581 info
->uses_instanceid
= TRUE
;
583 case TGSI_SEMANTIC_VERTEXID
:
584 info
->uses_vertexid
= TRUE
;
586 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
587 info
->uses_vertexid_nobase
= TRUE
;
589 case TGSI_SEMANTIC_BASEVERTEX
:
590 info
->uses_basevertex
= TRUE
;
592 case TGSI_SEMANTIC_PRIMID
:
593 info
->uses_primid
= TRUE
;
595 case TGSI_SEMANTIC_INVOCATIONID
:
596 info
->uses_invocationid
= TRUE
;
598 case TGSI_SEMANTIC_POSITION
:
599 info
->reads_position
= TRUE
;
601 case TGSI_SEMANTIC_FACE
:
602 info
->uses_frontface
= TRUE
;
604 case TGSI_SEMANTIC_SAMPLEMASK
:
605 info
->reads_samplemask
= TRUE
;
607 case TGSI_SEMANTIC_TESSINNER
:
608 case TGSI_SEMANTIC_TESSOUTER
:
609 info
->reads_tess_factors
= true;
614 case TGSI_FILE_OUTPUT
:
615 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
616 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
617 info
->output_usagemask
[reg
] |= fulldecl
->Declaration
.UsageMask
;
618 info
->num_outputs
= MAX2(info
->num_outputs
, reg
+ 1);
620 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_X
) {
621 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamX
;
622 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamX
]++;
624 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Y
) {
625 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamY
<< 2;
626 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamY
]++;
628 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Z
) {
629 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamZ
<< 4;
630 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamZ
]++;
632 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_W
) {
633 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamW
<< 6;
634 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamW
]++;
638 case TGSI_SEMANTIC_PRIMID
:
639 info
->writes_primid
= true;
641 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
642 info
->writes_viewport_index
= true;
644 case TGSI_SEMANTIC_LAYER
:
645 info
->writes_layer
= true;
647 case TGSI_SEMANTIC_PSIZE
:
648 info
->writes_psize
= true;
650 case TGSI_SEMANTIC_CLIPVERTEX
:
651 info
->writes_clipvertex
= true;
653 case TGSI_SEMANTIC_COLOR
:
654 info
->colors_written
|= 1 << semIndex
;
656 case TGSI_SEMANTIC_STENCIL
:
657 info
->writes_stencil
= true;
659 case TGSI_SEMANTIC_SAMPLEMASK
:
660 info
->writes_samplemask
= true;
662 case TGSI_SEMANTIC_EDGEFLAG
:
663 info
->writes_edgeflag
= true;
665 case TGSI_SEMANTIC_POSITION
:
666 if (procType
== PIPE_SHADER_FRAGMENT
)
667 info
->writes_z
= true;
669 info
->writes_position
= true;
674 case TGSI_FILE_SAMPLER
:
675 STATIC_ASSERT(sizeof(info
->samplers_declared
) * 8 >= PIPE_MAX_SAMPLERS
);
676 info
->samplers_declared
|= 1u << reg
;
679 case TGSI_FILE_SAMPLER_VIEW
:
680 target
= fulldecl
->SamplerView
.Resource
;
681 type
= fulldecl
->SamplerView
.ReturnTypeX
;
683 assert(target
< TGSI_TEXTURE_UNKNOWN
);
684 if (info
->sampler_targets
[reg
] == TGSI_TEXTURE_UNKNOWN
) {
685 /* Save sampler target for this sampler index */
686 info
->sampler_targets
[reg
] = target
;
687 info
->sampler_type
[reg
] = type
;
689 /* if previously declared, make sure targets agree */
690 assert(info
->sampler_targets
[reg
] == target
);
691 assert(info
->sampler_type
[reg
] == type
);
700 scan_immediate(struct tgsi_shader_info
*info
)
702 uint reg
= info
->immediate_count
++;
703 uint file
= TGSI_FILE_IMMEDIATE
;
705 info
->file_mask
[file
] |= (1 << reg
);
706 info
->file_count
[file
]++;
707 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
712 scan_property(struct tgsi_shader_info
*info
,
713 const struct tgsi_full_property
*fullprop
)
715 unsigned name
= fullprop
->Property
.PropertyName
;
716 unsigned value
= fullprop
->u
[0].Data
;
718 assert(name
< ARRAY_SIZE(info
->properties
));
719 info
->properties
[name
] = value
;
722 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
723 info
->num_written_clipdistance
= value
;
724 info
->clipdist_writemask
|= (1 << value
) - 1;
726 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
727 info
->num_written_culldistance
= value
;
728 info
->culldist_writemask
|= (1 << value
) - 1;
735 * Scan the given TGSI shader to collect information such as number of
736 * registers used, special instructions used, etc.
737 * \return info the result of the scan
740 tgsi_scan_shader(const struct tgsi_token
*tokens
,
741 struct tgsi_shader_info
*info
)
744 struct tgsi_parse_context parse
;
745 unsigned current_depth
= 0;
747 memset(info
, 0, sizeof(*info
));
748 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
749 info
->file_max
[i
] = -1;
750 for (i
= 0; i
< ARRAY_SIZE(info
->const_file_max
); i
++)
751 info
->const_file_max
[i
] = -1;
752 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
753 for (i
= 0; i
< ARRAY_SIZE(info
->sampler_targets
); i
++)
754 info
->sampler_targets
[i
] = TGSI_TEXTURE_UNKNOWN
;
757 ** Setup to begin parsing input shader
759 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
760 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
763 procType
= parse
.FullHeader
.Processor
.Processor
;
764 assert(procType
== PIPE_SHADER_FRAGMENT
||
765 procType
== PIPE_SHADER_VERTEX
||
766 procType
== PIPE_SHADER_GEOMETRY
||
767 procType
== PIPE_SHADER_TESS_CTRL
||
768 procType
== PIPE_SHADER_TESS_EVAL
||
769 procType
== PIPE_SHADER_COMPUTE
);
770 info
->processor
= procType
;
773 ** Loop over incoming program tokens/instructions
775 while (!tgsi_parse_end_of_tokens(&parse
)) {
778 tgsi_parse_token( &parse
);
780 switch( parse
.FullToken
.Token
.Type
) {
781 case TGSI_TOKEN_TYPE_INSTRUCTION
:
782 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
785 case TGSI_TOKEN_TYPE_DECLARATION
:
786 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
788 case TGSI_TOKEN_TYPE_IMMEDIATE
:
789 scan_immediate(info
);
791 case TGSI_TOKEN_TYPE_PROPERTY
:
792 scan_property(info
, &parse
.FullToken
.FullProperty
);
795 assert(!"Unexpected TGSI token type");
799 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
800 info
->opcode_count
[TGSI_OPCODE_KILL
]);
802 /* The dimensions of the IN decleration in geometry shader have
803 * to be deduced from the type of the input primitive.
805 if (procType
== PIPE_SHADER_GEOMETRY
) {
806 unsigned input_primitive
=
807 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
808 int num_verts
= u_vertices_per_prim(input_primitive
);
810 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
811 info
->file_max
[TGSI_FILE_INPUT
] =
812 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
813 for (j
= 0; j
< num_verts
; ++j
) {
814 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
818 tgsi_parse_free(&parse
);
822 * Collect information about the arrays of a given register file.
824 * @param tokens TGSI shader
825 * @param file the register file to scan through
826 * @param max_array_id number of entries in @p arrays; should be equal to the
827 * highest array id, i.e. tgsi_shader_info::array_max[file].
828 * @param arrays info for array of each ID will be written to arrays[ID - 1].
831 tgsi_scan_arrays(const struct tgsi_token
*tokens
,
833 unsigned max_array_id
,
834 struct tgsi_array_info
*arrays
)
836 struct tgsi_parse_context parse
;
838 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
839 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
843 memset(arrays
, 0, sizeof(arrays
[0]) * max_array_id
);
845 while (!tgsi_parse_end_of_tokens(&parse
)) {
846 struct tgsi_full_instruction
*inst
;
848 tgsi_parse_token(&parse
);
850 if (parse
.FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
851 struct tgsi_full_declaration
*decl
= &parse
.FullToken
.FullDeclaration
;
853 if (decl
->Declaration
.Array
&& decl
->Declaration
.File
== file
&&
854 decl
->Array
.ArrayID
> 0 && decl
->Array
.ArrayID
<= max_array_id
) {
855 struct tgsi_array_info
*array
= &arrays
[decl
->Array
.ArrayID
- 1];
856 assert(!array
->declared
);
857 array
->declared
= true;
858 array
->range
= decl
->Range
;
862 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
865 inst
= &parse
.FullToken
.FullInstruction
;
866 for (unsigned i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
867 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
868 if (dst
->Register
.File
!= file
)
871 if (dst
->Register
.Indirect
) {
872 if (dst
->Indirect
.ArrayID
> 0 &&
873 dst
->Indirect
.ArrayID
<= max_array_id
) {
874 arrays
[dst
->Indirect
.ArrayID
- 1].writemask
|= dst
->Register
.WriteMask
;
876 /* Indirect writes without an ArrayID can write anywhere. */
877 for (unsigned j
= 0; j
< max_array_id
; ++j
)
878 arrays
[j
].writemask
|= dst
->Register
.WriteMask
;
881 /* Check whether the write falls into any of the arrays anyway. */
882 for (unsigned j
= 0; j
< max_array_id
; ++j
) {
883 struct tgsi_array_info
*array
= &arrays
[j
];
884 if (array
->declared
&&
885 dst
->Register
.Index
>= array
->range
.First
&&
886 dst
->Register
.Index
<= array
->range
.Last
)
887 array
->writemask
|= dst
->Register
.WriteMask
;
893 tgsi_parse_free(&parse
);