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 /* Mark which inputs are effectively used */
119 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
120 if (src
->Register
.Indirect
) {
121 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
122 info
->input_usage_mask
[ind
] |= usage_mask
;
126 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
127 info
->input_usage_mask
[ind
] |= usage_mask
;
130 if (info
->processor
== PIPE_SHADER_FRAGMENT
) {
131 unsigned name
, index
, input
;
133 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
134 input
= info
->input_array_first
[src
->Indirect
.ArrayID
];
136 input
= src
->Register
.Index
;
138 name
= info
->input_semantic_name
[input
];
139 index
= info
->input_semantic_index
[input
];
141 if (name
== TGSI_SEMANTIC_POSITION
&&
142 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
143 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
144 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
145 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
))
146 info
->reads_z
= TRUE
;
148 if (name
== TGSI_SEMANTIC_COLOR
) {
150 (1 << src
->Register
.SwizzleX
) |
151 (1 << src
->Register
.SwizzleY
) |
152 (1 << src
->Register
.SwizzleZ
) |
153 (1 << src
->Register
.SwizzleW
);
155 info
->colors_read
|= mask
<< (index
* 4);
158 /* Process only interpolated varyings. Don't include POSITION.
159 * Don't include integer varyings, because they are not
160 * interpolated. Don't process inputs interpolated by INTERP
161 * opcodes. Those are tracked separately.
163 if ((!is_interp_instruction
|| src_index
!= 0) &&
164 (name
== TGSI_SEMANTIC_GENERIC
||
165 name
== TGSI_SEMANTIC_TEXCOORD
||
166 name
== TGSI_SEMANTIC_COLOR
||
167 name
== TGSI_SEMANTIC_BCOLOR
||
168 name
== TGSI_SEMANTIC_FOG
||
169 name
== TGSI_SEMANTIC_CLIPDIST
)) {
170 switch (info
->input_interpolate
[input
]) {
171 case TGSI_INTERPOLATE_COLOR
:
172 case TGSI_INTERPOLATE_PERSPECTIVE
:
173 switch (info
->input_interpolate_loc
[input
]) {
174 case TGSI_INTERPOLATE_LOC_CENTER
:
175 info
->uses_persp_center
= TRUE
;
177 case TGSI_INTERPOLATE_LOC_CENTROID
:
178 info
->uses_persp_centroid
= TRUE
;
180 case TGSI_INTERPOLATE_LOC_SAMPLE
:
181 info
->uses_persp_sample
= TRUE
;
185 case TGSI_INTERPOLATE_LINEAR
:
186 switch (info
->input_interpolate_loc
[input
]) {
187 case TGSI_INTERPOLATE_LOC_CENTER
:
188 info
->uses_linear_center
= TRUE
;
190 case TGSI_INTERPOLATE_LOC_CENTROID
:
191 info
->uses_linear_centroid
= TRUE
;
193 case TGSI_INTERPOLATE_LOC_SAMPLE
:
194 info
->uses_linear_sample
= TRUE
;
198 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
204 if (info
->processor
== PIPE_SHADER_TESS_CTRL
&&
205 src
->Register
.File
== TGSI_FILE_OUTPUT
) {
208 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
209 input
= info
->output_array_first
[src
->Indirect
.ArrayID
];
211 input
= src
->Register
.Index
;
213 switch (info
->output_semantic_name
[input
]) {
214 case TGSI_SEMANTIC_PATCH
:
215 info
->reads_perpatch_outputs
= true;
217 case TGSI_SEMANTIC_TESSINNER
:
218 case TGSI_SEMANTIC_TESSOUTER
:
219 info
->reads_tessfactor_outputs
= true;
222 info
->reads_pervertex_outputs
= true;
226 /* check for indirect register reads */
227 if (src
->Register
.Indirect
) {
228 info
->indirect_files
|= (1 << src
->Register
.File
);
229 info
->indirect_files_read
|= (1 << src
->Register
.File
);
231 /* record indirect constant buffer indexing */
232 if (src
->Register
.File
== TGSI_FILE_CONSTANT
) {
233 if (src
->Register
.Dimension
) {
234 if (src
->Dimension
.Indirect
)
235 info
->const_buffers_indirect
= info
->const_buffers_declared
;
237 info
->const_buffers_indirect
|= 1u << src
->Dimension
.Index
;
239 info
->const_buffers_indirect
|= 1;
244 if (src
->Register
.Dimension
&& src
->Dimension
.Indirect
)
245 info
->dim_indirect_files
|= 1u << src
->Register
.File
;
247 /* Texture samplers */
248 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
249 const unsigned index
= src
->Register
.Index
;
251 assert(fullinst
->Instruction
.Texture
);
252 assert(index
< ARRAY_SIZE(info
->is_msaa_sampler
));
253 assert(index
< PIPE_MAX_SAMPLERS
);
255 if (is_texture_inst(fullinst
->Instruction
.Opcode
)) {
256 const unsigned target
= fullinst
->Texture
.Texture
;
257 assert(target
< TGSI_TEXTURE_UNKNOWN
);
258 /* for texture instructions, check that the texture instruction
259 * target matches the previous sampler view declaration (if there
262 if (info
->sampler_targets
[index
] == TGSI_TEXTURE_UNKNOWN
) {
263 /* probably no sampler view declaration */
264 info
->sampler_targets
[index
] = target
;
266 /* Make sure the texture instruction's sampler/target info
267 * agrees with the sampler view declaration.
269 assert(info
->sampler_targets
[index
] == target
);
272 if (target
== TGSI_TEXTURE_2D_MSAA
||
273 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
274 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
279 if (is_memory_file(src
->Register
.File
) &&
280 !is_mem_query_inst(fullinst
->Instruction
.Opcode
)) {
283 if (tgsi_get_opcode_info(fullinst
->Instruction
.Opcode
)->is_store
) {
284 info
->writes_memory
= TRUE
;
286 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
287 if (src
->Register
.Indirect
)
288 info
->images_atomic
= info
->images_declared
;
290 info
->images_atomic
|= 1 << src
->Register
.Index
;
291 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
292 if (src
->Register
.Indirect
)
293 info
->shader_buffers_atomic
= info
->shader_buffers_declared
;
295 info
->shader_buffers_atomic
|= 1 << src
->Register
.Index
;
298 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
299 if (src
->Register
.Indirect
)
300 info
->images_load
= info
->images_declared
;
302 info
->images_load
|= 1 << src
->Register
.Index
;
303 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
304 if (src
->Register
.Indirect
)
305 info
->shader_buffers_load
= info
->shader_buffers_declared
;
307 info
->shader_buffers_load
|= 1 << src
->Register
.Index
;
315 scan_instruction(struct tgsi_shader_info
*info
,
316 const struct tgsi_full_instruction
*fullinst
,
317 unsigned *current_depth
)
320 bool is_mem_inst
= false;
321 bool is_interp_instruction
= false;
323 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
324 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
326 switch (fullinst
->Instruction
.Opcode
) {
328 case TGSI_OPCODE_UIF
:
329 case TGSI_OPCODE_BGNLOOP
:
331 info
->max_depth
= MAX2(info
->max_depth
, *current_depth
);
333 case TGSI_OPCODE_ENDIF
:
334 case TGSI_OPCODE_ENDLOOP
:
341 if (fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_CENTROID
||
342 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
343 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
344 const struct tgsi_full_src_register
*src0
= &fullinst
->Src
[0];
347 is_interp_instruction
= true;
349 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
350 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
352 input
= src0
->Register
.Index
;
354 /* For the INTERP opcodes, the interpolation is always
355 * PERSPECTIVE unless LINEAR is specified.
357 switch (info
->input_interpolate
[input
]) {
358 case TGSI_INTERPOLATE_COLOR
:
359 case TGSI_INTERPOLATE_CONSTANT
:
360 case TGSI_INTERPOLATE_PERSPECTIVE
:
361 switch (fullinst
->Instruction
.Opcode
) {
362 case TGSI_OPCODE_INTERP_CENTROID
:
363 info
->uses_persp_opcode_interp_centroid
= TRUE
;
365 case TGSI_OPCODE_INTERP_OFFSET
:
366 info
->uses_persp_opcode_interp_offset
= TRUE
;
368 case TGSI_OPCODE_INTERP_SAMPLE
:
369 info
->uses_persp_opcode_interp_sample
= TRUE
;
374 case TGSI_INTERPOLATE_LINEAR
:
375 switch (fullinst
->Instruction
.Opcode
) {
376 case TGSI_OPCODE_INTERP_CENTROID
:
377 info
->uses_linear_opcode_interp_centroid
= TRUE
;
379 case TGSI_OPCODE_INTERP_OFFSET
:
380 info
->uses_linear_opcode_interp_offset
= TRUE
;
382 case TGSI_OPCODE_INTERP_SAMPLE
:
383 info
->uses_linear_opcode_interp_sample
= TRUE
;
390 if (fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
391 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
)
392 info
->uses_doubles
= TRUE
;
394 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
395 scan_src_operand(info
, fullinst
, &fullinst
->Src
[i
], i
,
396 tgsi_util_get_inst_usage_mask(fullinst
, i
),
397 is_interp_instruction
, &is_mem_inst
);
400 if (fullinst
->Instruction
.Texture
) {
401 for (i
= 0; i
< fullinst
->Texture
.NumOffsets
; i
++) {
402 struct tgsi_full_src_register src
= {{0}};
404 src
.Register
.File
= fullinst
->TexOffsets
[i
].File
;
405 src
.Register
.Index
= fullinst
->TexOffsets
[i
].Index
;
406 src
.Register
.SwizzleX
= fullinst
->TexOffsets
[i
].SwizzleX
;
407 src
.Register
.SwizzleY
= fullinst
->TexOffsets
[i
].SwizzleY
;
408 src
.Register
.SwizzleZ
= fullinst
->TexOffsets
[i
].SwizzleZ
;
410 /* The usage mask is suboptimal but should be safe. */
411 scan_src_operand(info
, fullinst
, &src
, 0, TGSI_WRITEMASK_XYZ
,
412 false, &is_mem_inst
);
416 /* check for indirect register writes */
417 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
418 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
419 if (dst
->Register
.Indirect
) {
420 info
->indirect_files
|= (1 << dst
->Register
.File
);
421 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
424 if (dst
->Register
.Dimension
&& dst
->Dimension
.Indirect
)
425 info
->dim_indirect_files
|= 1u << dst
->Register
.File
;
427 if (is_memory_file(dst
->Register
.File
)) {
428 assert(fullinst
->Instruction
.Opcode
== TGSI_OPCODE_STORE
);
431 info
->writes_memory
= TRUE
;
433 if (dst
->Register
.File
== TGSI_FILE_IMAGE
) {
434 if (dst
->Register
.Indirect
)
435 info
->images_store
= info
->images_declared
;
437 info
->images_store
|= 1 << dst
->Register
.Index
;
438 } else if (dst
->Register
.File
== TGSI_FILE_BUFFER
) {
439 if (dst
->Register
.Indirect
)
440 info
->shader_buffers_store
= info
->shader_buffers_declared
;
442 info
->shader_buffers_store
|= 1 << dst
->Register
.Index
;
448 info
->num_memory_instructions
++;
450 if (computes_derivative(fullinst
->Instruction
.Opcode
))
451 info
->uses_derivatives
= true;
453 info
->num_instructions
++;
458 scan_declaration(struct tgsi_shader_info
*info
,
459 const struct tgsi_full_declaration
*fulldecl
)
461 const uint file
= fulldecl
->Declaration
.File
;
462 const unsigned procType
= info
->processor
;
465 if (fulldecl
->Declaration
.Array
) {
466 unsigned array_id
= fulldecl
->Array
.ArrayID
;
469 case TGSI_FILE_INPUT
:
470 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
471 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
472 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
474 case TGSI_FILE_OUTPUT
:
475 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
476 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
477 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
480 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
483 for (reg
= fulldecl
->Range
.First
; reg
<= fulldecl
->Range
.Last
; reg
++) {
484 unsigned semName
= fulldecl
->Semantic
.Name
;
485 unsigned semIndex
= fulldecl
->Semantic
.Index
+
486 (reg
- fulldecl
->Range
.First
);
488 unsigned index
, target
, type
;
490 /* only first 32 regs will appear in this bitfield */
491 info
->file_mask
[file
] |= (1 << reg
);
492 info
->file_count
[file
]++;
493 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
496 case TGSI_FILE_CONSTANT
:
499 if (fulldecl
->Declaration
.Dimension
)
500 buffer
= fulldecl
->Dim
.Index2D
;
502 info
->const_file_max
[buffer
] =
503 MAX2(info
->const_file_max
[buffer
], (int)reg
);
504 info
->const_buffers_declared
|= 1u << buffer
;
507 case TGSI_FILE_IMAGE
:
508 info
->images_declared
|= 1u << reg
;
509 if (fulldecl
->Image
.Resource
== TGSI_TEXTURE_BUFFER
)
510 info
->images_buffers
|= 1 << reg
;
513 case TGSI_FILE_BUFFER
:
514 info
->shader_buffers_declared
|= 1u << reg
;
517 case TGSI_FILE_INPUT
:
518 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
519 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
520 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
521 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
522 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
524 /* Vertex shaders can have inputs with holes between them. */
525 info
->num_inputs
= MAX2(info
->num_inputs
, reg
+ 1);
527 if (semName
== TGSI_SEMANTIC_PRIMID
)
528 info
->uses_primid
= TRUE
;
529 else if (procType
== PIPE_SHADER_FRAGMENT
) {
530 if (semName
== TGSI_SEMANTIC_POSITION
)
531 info
->reads_position
= TRUE
;
532 else if (semName
== TGSI_SEMANTIC_FACE
)
533 info
->uses_frontface
= TRUE
;
537 case TGSI_FILE_SYSTEM_VALUE
:
538 index
= fulldecl
->Range
.First
;
540 info
->system_value_semantic_name
[index
] = semName
;
541 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
544 case TGSI_SEMANTIC_INSTANCEID
:
545 info
->uses_instanceid
= TRUE
;
547 case TGSI_SEMANTIC_VERTEXID
:
548 info
->uses_vertexid
= TRUE
;
550 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
551 info
->uses_vertexid_nobase
= TRUE
;
553 case TGSI_SEMANTIC_BASEVERTEX
:
554 info
->uses_basevertex
= TRUE
;
556 case TGSI_SEMANTIC_PRIMID
:
557 info
->uses_primid
= TRUE
;
559 case TGSI_SEMANTIC_INVOCATIONID
:
560 info
->uses_invocationid
= TRUE
;
562 case TGSI_SEMANTIC_POSITION
:
563 info
->reads_position
= TRUE
;
565 case TGSI_SEMANTIC_FACE
:
566 info
->uses_frontface
= TRUE
;
568 case TGSI_SEMANTIC_SAMPLEMASK
:
569 info
->reads_samplemask
= TRUE
;
571 case TGSI_SEMANTIC_TESSINNER
:
572 case TGSI_SEMANTIC_TESSOUTER
:
573 info
->reads_tess_factors
= true;
578 case TGSI_FILE_OUTPUT
:
579 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
580 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
581 info
->output_usagemask
[reg
] |= fulldecl
->Declaration
.UsageMask
;
582 info
->num_outputs
= MAX2(info
->num_outputs
, reg
+ 1);
584 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_X
) {
585 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamX
;
586 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamX
]++;
588 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Y
) {
589 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamY
<< 2;
590 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamY
]++;
592 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Z
) {
593 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamZ
<< 4;
594 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamZ
]++;
596 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_W
) {
597 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamW
<< 6;
598 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamW
]++;
602 case TGSI_SEMANTIC_PRIMID
:
603 info
->writes_primid
= true;
605 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
606 info
->writes_viewport_index
= true;
608 case TGSI_SEMANTIC_LAYER
:
609 info
->writes_layer
= true;
611 case TGSI_SEMANTIC_PSIZE
:
612 info
->writes_psize
= true;
614 case TGSI_SEMANTIC_CLIPVERTEX
:
615 info
->writes_clipvertex
= true;
617 case TGSI_SEMANTIC_COLOR
:
618 info
->colors_written
|= 1 << semIndex
;
620 case TGSI_SEMANTIC_STENCIL
:
621 info
->writes_stencil
= true;
623 case TGSI_SEMANTIC_SAMPLEMASK
:
624 info
->writes_samplemask
= true;
626 case TGSI_SEMANTIC_EDGEFLAG
:
627 info
->writes_edgeflag
= true;
629 case TGSI_SEMANTIC_POSITION
:
630 if (procType
== PIPE_SHADER_FRAGMENT
)
631 info
->writes_z
= true;
633 info
->writes_position
= true;
638 case TGSI_FILE_SAMPLER
:
639 STATIC_ASSERT(sizeof(info
->samplers_declared
) * 8 >= PIPE_MAX_SAMPLERS
);
640 info
->samplers_declared
|= 1u << reg
;
643 case TGSI_FILE_SAMPLER_VIEW
:
644 target
= fulldecl
->SamplerView
.Resource
;
645 type
= fulldecl
->SamplerView
.ReturnTypeX
;
647 assert(target
< TGSI_TEXTURE_UNKNOWN
);
648 if (info
->sampler_targets
[reg
] == TGSI_TEXTURE_UNKNOWN
) {
649 /* Save sampler target for this sampler index */
650 info
->sampler_targets
[reg
] = target
;
651 info
->sampler_type
[reg
] = type
;
653 /* if previously declared, make sure targets agree */
654 assert(info
->sampler_targets
[reg
] == target
);
655 assert(info
->sampler_type
[reg
] == type
);
664 scan_immediate(struct tgsi_shader_info
*info
)
666 uint reg
= info
->immediate_count
++;
667 uint file
= TGSI_FILE_IMMEDIATE
;
669 info
->file_mask
[file
] |= (1 << reg
);
670 info
->file_count
[file
]++;
671 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
676 scan_property(struct tgsi_shader_info
*info
,
677 const struct tgsi_full_property
*fullprop
)
679 unsigned name
= fullprop
->Property
.PropertyName
;
680 unsigned value
= fullprop
->u
[0].Data
;
682 assert(name
< ARRAY_SIZE(info
->properties
));
683 info
->properties
[name
] = value
;
686 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
687 info
->num_written_clipdistance
= value
;
688 info
->clipdist_writemask
|= (1 << value
) - 1;
690 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
691 info
->num_written_culldistance
= value
;
692 info
->culldist_writemask
|= (1 << value
) - 1;
699 * Scan the given TGSI shader to collect information such as number of
700 * registers used, special instructions used, etc.
701 * \return info the result of the scan
704 tgsi_scan_shader(const struct tgsi_token
*tokens
,
705 struct tgsi_shader_info
*info
)
708 struct tgsi_parse_context parse
;
709 unsigned current_depth
= 0;
711 memset(info
, 0, sizeof(*info
));
712 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
713 info
->file_max
[i
] = -1;
714 for (i
= 0; i
< ARRAY_SIZE(info
->const_file_max
); i
++)
715 info
->const_file_max
[i
] = -1;
716 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
717 for (i
= 0; i
< ARRAY_SIZE(info
->sampler_targets
); i
++)
718 info
->sampler_targets
[i
] = TGSI_TEXTURE_UNKNOWN
;
721 ** Setup to begin parsing input shader
723 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
724 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
727 procType
= parse
.FullHeader
.Processor
.Processor
;
728 assert(procType
== PIPE_SHADER_FRAGMENT
||
729 procType
== PIPE_SHADER_VERTEX
||
730 procType
== PIPE_SHADER_GEOMETRY
||
731 procType
== PIPE_SHADER_TESS_CTRL
||
732 procType
== PIPE_SHADER_TESS_EVAL
||
733 procType
== PIPE_SHADER_COMPUTE
);
734 info
->processor
= procType
;
737 ** Loop over incoming program tokens/instructions
739 while (!tgsi_parse_end_of_tokens(&parse
)) {
742 tgsi_parse_token( &parse
);
744 switch( parse
.FullToken
.Token
.Type
) {
745 case TGSI_TOKEN_TYPE_INSTRUCTION
:
746 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
749 case TGSI_TOKEN_TYPE_DECLARATION
:
750 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
752 case TGSI_TOKEN_TYPE_IMMEDIATE
:
753 scan_immediate(info
);
755 case TGSI_TOKEN_TYPE_PROPERTY
:
756 scan_property(info
, &parse
.FullToken
.FullProperty
);
759 assert(!"Unexpected TGSI token type");
763 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
764 info
->opcode_count
[TGSI_OPCODE_KILL
]);
766 /* The dimensions of the IN decleration in geometry shader have
767 * to be deduced from the type of the input primitive.
769 if (procType
== PIPE_SHADER_GEOMETRY
) {
770 unsigned input_primitive
=
771 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
772 int num_verts
= u_vertices_per_prim(input_primitive
);
774 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
775 info
->file_max
[TGSI_FILE_INPUT
] =
776 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
777 for (j
= 0; j
< num_verts
; ++j
) {
778 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
782 tgsi_parse_free(&parse
);
786 * Collect information about the arrays of a given register file.
788 * @param tokens TGSI shader
789 * @param file the register file to scan through
790 * @param max_array_id number of entries in @p arrays; should be equal to the
791 * highest array id, i.e. tgsi_shader_info::array_max[file].
792 * @param arrays info for array of each ID will be written to arrays[ID - 1].
795 tgsi_scan_arrays(const struct tgsi_token
*tokens
,
797 unsigned max_array_id
,
798 struct tgsi_array_info
*arrays
)
800 struct tgsi_parse_context parse
;
802 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
803 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
807 memset(arrays
, 0, sizeof(arrays
[0]) * max_array_id
);
809 while (!tgsi_parse_end_of_tokens(&parse
)) {
810 struct tgsi_full_instruction
*inst
;
812 tgsi_parse_token(&parse
);
814 if (parse
.FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
815 struct tgsi_full_declaration
*decl
= &parse
.FullToken
.FullDeclaration
;
817 if (decl
->Declaration
.Array
&& decl
->Declaration
.File
== file
&&
818 decl
->Array
.ArrayID
> 0 && decl
->Array
.ArrayID
<= max_array_id
) {
819 struct tgsi_array_info
*array
= &arrays
[decl
->Array
.ArrayID
- 1];
820 assert(!array
->declared
);
821 array
->declared
= true;
822 array
->range
= decl
->Range
;
826 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
829 inst
= &parse
.FullToken
.FullInstruction
;
830 for (unsigned i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
831 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
832 if (dst
->Register
.File
!= file
)
835 if (dst
->Register
.Indirect
) {
836 if (dst
->Indirect
.ArrayID
> 0 &&
837 dst
->Indirect
.ArrayID
<= max_array_id
) {
838 arrays
[dst
->Indirect
.ArrayID
- 1].writemask
|= dst
->Register
.WriteMask
;
840 /* Indirect writes without an ArrayID can write anywhere. */
841 for (unsigned j
= 0; j
< max_array_id
; ++j
)
842 arrays
[j
].writemask
|= dst
->Register
.WriteMask
;
845 /* Check whether the write falls into any of the arrays anyway. */
846 for (unsigned j
= 0; j
< max_array_id
; ++j
) {
847 struct tgsi_array_info
*array
= &arrays
[j
];
848 if (array
->declared
&&
849 dst
->Register
.Index
>= array
->range
.First
&&
850 dst
->Register
.Index
<= array
->range
.Last
)
851 array
->writemask
|= dst
->Register
.WriteMask
;
857 tgsi_parse_free(&parse
);
864 * Check if the given shader is a "passthrough" shader consisting of only
865 * MOV instructions of the form: MOV OUT[n], IN[n]
869 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
871 struct tgsi_parse_context parse
;
874 ** Setup to begin parsing input shader
876 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
877 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
882 ** Loop over incoming program tokens/instructions
884 while (!tgsi_parse_end_of_tokens(&parse
)) {
886 tgsi_parse_token(&parse
);
888 switch (parse
.FullToken
.Token
.Type
) {
889 case TGSI_TOKEN_TYPE_INSTRUCTION
:
891 struct tgsi_full_instruction
*fullinst
=
892 &parse
.FullToken
.FullInstruction
;
893 const struct tgsi_full_src_register
*src
=
895 const struct tgsi_full_dst_register
*dst
=
898 /* Do a whole bunch of checks for a simple move */
899 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
900 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
901 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
902 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
903 src
->Register
.Index
!= dst
->Register
.Index
||
905 src
->Register
.Negate
||
906 src
->Register
.Absolute
||
908 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
909 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
910 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
911 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
913 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
915 tgsi_parse_free(&parse
);
921 case TGSI_TOKEN_TYPE_DECLARATION
:
923 case TGSI_TOKEN_TYPE_IMMEDIATE
:
925 case TGSI_TOKEN_TYPE_PROPERTY
:
932 tgsi_parse_free(&parse
);
934 /* if we get here, it's a pass-through shader */