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_LODQ
;
67 * Is the opcode a "true" texture instruction which samples from a
71 is_texture_inst(unsigned opcode
)
73 return (!is_mem_query_inst(opcode
) &&
74 tgsi_get_opcode_info(opcode
)->is_tex
);
79 * Is the opcode an instruction which computes a derivative explicitly or
83 computes_derivative(unsigned opcode
)
85 if (tgsi_get_opcode_info(opcode
)->is_tex
) {
86 return opcode
!= TGSI_OPCODE_TG4
&&
87 opcode
!= TGSI_OPCODE_TXD
&&
88 opcode
!= TGSI_OPCODE_TXF
&&
89 opcode
!= TGSI_OPCODE_TXF_LZ
&&
90 opcode
!= TGSI_OPCODE_TEX_LZ
&&
91 opcode
!= TGSI_OPCODE_TXL
&&
92 opcode
!= TGSI_OPCODE_TXL2
&&
93 opcode
!= TGSI_OPCODE_TXQ
&&
94 opcode
!= TGSI_OPCODE_TXQS
;
97 return opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
||
98 opcode
== TGSI_OPCODE_DDY
|| opcode
== TGSI_OPCODE_DDY_FINE
||
99 opcode
== TGSI_OPCODE_SAMPLE
||
100 opcode
== TGSI_OPCODE_SAMPLE_B
||
101 opcode
== TGSI_OPCODE_SAMPLE_C
;
106 scan_src_operand(struct tgsi_shader_info
*info
,
107 const struct tgsi_full_instruction
*fullinst
,
108 const struct tgsi_full_src_register
*src
,
111 bool is_interp_instruction
,
114 int ind
= src
->Register
.Index
;
116 if (info
->processor
== PIPE_SHADER_COMPUTE
&&
117 src
->Register
.File
== TGSI_FILE_SYSTEM_VALUE
) {
118 unsigned swizzle
[4], i
, name
;
120 name
= info
->system_value_semantic_name
[src
->Register
.Index
];
121 swizzle
[0] = src
->Register
.SwizzleX
;
122 swizzle
[1] = src
->Register
.SwizzleY
;
123 swizzle
[2] = src
->Register
.SwizzleZ
;
124 swizzle
[3] = src
->Register
.SwizzleW
;
127 case TGSI_SEMANTIC_THREAD_ID
:
128 case TGSI_SEMANTIC_BLOCK_ID
:
129 for (i
= 0; i
< 4; i
++) {
130 if (swizzle
[i
] <= TGSI_SWIZZLE_Z
) {
131 if (name
== TGSI_SEMANTIC_THREAD_ID
)
132 info
->uses_thread_id
[swizzle
[i
]] = true;
134 info
->uses_block_id
[swizzle
[i
]] = true;
138 case TGSI_SEMANTIC_BLOCK_SIZE
:
139 /* The block size is translated to IMM with a fixed block size. */
140 if (info
->properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0)
141 info
->uses_block_size
= true;
143 case TGSI_SEMANTIC_GRID_SIZE
:
144 info
->uses_grid_size
= true;
149 /* Mark which inputs are effectively used */
150 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
151 if (src
->Register
.Indirect
) {
152 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
153 info
->input_usage_mask
[ind
] |= usage_mask
;
157 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
158 info
->input_usage_mask
[ind
] |= usage_mask
;
161 if (info
->processor
== PIPE_SHADER_FRAGMENT
) {
162 unsigned name
, index
, input
;
164 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
165 input
= info
->input_array_first
[src
->Indirect
.ArrayID
];
167 input
= src
->Register
.Index
;
169 name
= info
->input_semantic_name
[input
];
170 index
= info
->input_semantic_index
[input
];
172 if (name
== TGSI_SEMANTIC_POSITION
&&
173 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
174 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
175 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
176 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
))
177 info
->reads_z
= TRUE
;
179 if (name
== TGSI_SEMANTIC_COLOR
) {
181 (1 << src
->Register
.SwizzleX
) |
182 (1 << src
->Register
.SwizzleY
) |
183 (1 << src
->Register
.SwizzleZ
) |
184 (1 << src
->Register
.SwizzleW
);
186 info
->colors_read
|= mask
<< (index
* 4);
189 /* Process only interpolated varyings. Don't include POSITION.
190 * Don't include integer varyings, because they are not
191 * interpolated. Don't process inputs interpolated by INTERP
192 * opcodes. Those are tracked separately.
194 if ((!is_interp_instruction
|| src_index
!= 0) &&
195 (name
== TGSI_SEMANTIC_GENERIC
||
196 name
== TGSI_SEMANTIC_TEXCOORD
||
197 name
== TGSI_SEMANTIC_COLOR
||
198 name
== TGSI_SEMANTIC_BCOLOR
||
199 name
== TGSI_SEMANTIC_FOG
||
200 name
== TGSI_SEMANTIC_CLIPDIST
)) {
201 switch (info
->input_interpolate
[input
]) {
202 case TGSI_INTERPOLATE_COLOR
:
203 case TGSI_INTERPOLATE_PERSPECTIVE
:
204 switch (info
->input_interpolate_loc
[input
]) {
205 case TGSI_INTERPOLATE_LOC_CENTER
:
206 info
->uses_persp_center
= TRUE
;
208 case TGSI_INTERPOLATE_LOC_CENTROID
:
209 info
->uses_persp_centroid
= TRUE
;
211 case TGSI_INTERPOLATE_LOC_SAMPLE
:
212 info
->uses_persp_sample
= TRUE
;
216 case TGSI_INTERPOLATE_LINEAR
:
217 switch (info
->input_interpolate_loc
[input
]) {
218 case TGSI_INTERPOLATE_LOC_CENTER
:
219 info
->uses_linear_center
= TRUE
;
221 case TGSI_INTERPOLATE_LOC_CENTROID
:
222 info
->uses_linear_centroid
= TRUE
;
224 case TGSI_INTERPOLATE_LOC_SAMPLE
:
225 info
->uses_linear_sample
= TRUE
;
229 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
235 if (info
->processor
== PIPE_SHADER_TESS_CTRL
&&
236 src
->Register
.File
== TGSI_FILE_OUTPUT
) {
239 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
240 input
= info
->output_array_first
[src
->Indirect
.ArrayID
];
242 input
= src
->Register
.Index
;
244 switch (info
->output_semantic_name
[input
]) {
245 case TGSI_SEMANTIC_PATCH
:
246 info
->reads_perpatch_outputs
= true;
248 case TGSI_SEMANTIC_TESSINNER
:
249 case TGSI_SEMANTIC_TESSOUTER
:
250 info
->reads_tessfactor_outputs
= true;
253 info
->reads_pervertex_outputs
= true;
257 /* check for indirect register reads */
258 if (src
->Register
.Indirect
) {
259 info
->indirect_files
|= (1 << src
->Register
.File
);
260 info
->indirect_files_read
|= (1 << src
->Register
.File
);
262 /* record indirect constant buffer indexing */
263 if (src
->Register
.File
== TGSI_FILE_CONSTANT
) {
264 if (src
->Register
.Dimension
) {
265 if (src
->Dimension
.Indirect
)
266 info
->const_buffers_indirect
= info
->const_buffers_declared
;
268 info
->const_buffers_indirect
|= 1u << src
->Dimension
.Index
;
270 info
->const_buffers_indirect
|= 1;
275 if (src
->Register
.Dimension
&& src
->Dimension
.Indirect
)
276 info
->dim_indirect_files
|= 1u << src
->Register
.File
;
278 /* Texture samplers */
279 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
280 const unsigned index
= src
->Register
.Index
;
282 assert(fullinst
->Instruction
.Texture
);
283 assert(index
< ARRAY_SIZE(info
->is_msaa_sampler
));
284 assert(index
< PIPE_MAX_SAMPLERS
);
286 if (is_texture_inst(fullinst
->Instruction
.Opcode
)) {
287 const unsigned target
= fullinst
->Texture
.Texture
;
288 assert(target
< TGSI_TEXTURE_UNKNOWN
);
289 /* for texture instructions, check that the texture instruction
290 * target matches the previous sampler view declaration (if there
293 if (info
->sampler_targets
[index
] == TGSI_TEXTURE_UNKNOWN
) {
294 /* probably no sampler view declaration */
295 info
->sampler_targets
[index
] = target
;
297 /* Make sure the texture instruction's sampler/target info
298 * agrees with the sampler view declaration.
300 assert(info
->sampler_targets
[index
] == target
);
303 if (target
== TGSI_TEXTURE_2D_MSAA
||
304 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
305 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
310 if (is_memory_file(src
->Register
.File
) &&
311 !is_mem_query_inst(fullinst
->Instruction
.Opcode
)) {
314 if (tgsi_get_opcode_info(fullinst
->Instruction
.Opcode
)->is_store
) {
315 info
->writes_memory
= TRUE
;
317 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
318 if (src
->Register
.Indirect
)
319 info
->images_atomic
= info
->images_declared
;
321 info
->images_atomic
|= 1 << src
->Register
.Index
;
322 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
323 if (src
->Register
.Indirect
)
324 info
->shader_buffers_atomic
= info
->shader_buffers_declared
;
326 info
->shader_buffers_atomic
|= 1 << src
->Register
.Index
;
329 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
330 if (src
->Register
.Indirect
)
331 info
->images_load
= info
->images_declared
;
333 info
->images_load
|= 1 << src
->Register
.Index
;
334 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
335 if (src
->Register
.Indirect
)
336 info
->shader_buffers_load
= info
->shader_buffers_declared
;
338 info
->shader_buffers_load
|= 1 << src
->Register
.Index
;
346 scan_instruction(struct tgsi_shader_info
*info
,
347 const struct tgsi_full_instruction
*fullinst
,
348 unsigned *current_depth
)
351 bool is_mem_inst
= false;
352 bool is_interp_instruction
= false;
353 unsigned sampler_src
;
355 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
356 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
358 switch (fullinst
->Instruction
.Opcode
) {
360 case TGSI_OPCODE_UIF
:
361 case TGSI_OPCODE_BGNLOOP
:
363 info
->max_depth
= MAX2(info
->max_depth
, *current_depth
);
365 case TGSI_OPCODE_ENDIF
:
366 case TGSI_OPCODE_ENDLOOP
:
369 case TGSI_OPCODE_TEX
:
370 case TGSI_OPCODE_TEX_LZ
:
371 case TGSI_OPCODE_TXB
:
372 case TGSI_OPCODE_TXD
:
373 case TGSI_OPCODE_TXL
:
374 case TGSI_OPCODE_TXP
:
375 case TGSI_OPCODE_TXQ
:
376 case TGSI_OPCODE_TXQS
:
377 case TGSI_OPCODE_TXF
:
378 case TGSI_OPCODE_TXF_LZ
:
379 case TGSI_OPCODE_TEX2
:
380 case TGSI_OPCODE_TXB2
:
381 case TGSI_OPCODE_TXL2
:
382 case TGSI_OPCODE_TG4
:
383 case TGSI_OPCODE_LODQ
:
384 sampler_src
= fullinst
->Instruction
.NumSrcRegs
- 1;
385 if (fullinst
->Src
[sampler_src
].Register
.File
!= TGSI_FILE_SAMPLER
)
386 info
->uses_bindless_samplers
= true;
388 case TGSI_OPCODE_RESQ
:
389 case TGSI_OPCODE_LOAD
:
390 case TGSI_OPCODE_ATOMUADD
:
391 case TGSI_OPCODE_ATOMXCHG
:
392 case TGSI_OPCODE_ATOMCAS
:
393 case TGSI_OPCODE_ATOMAND
:
394 case TGSI_OPCODE_ATOMOR
:
395 case TGSI_OPCODE_ATOMXOR
:
396 case TGSI_OPCODE_ATOMUMIN
:
397 case TGSI_OPCODE_ATOMUMAX
:
398 case TGSI_OPCODE_ATOMIMIN
:
399 case TGSI_OPCODE_ATOMIMAX
:
400 if (tgsi_is_bindless_image_file(fullinst
->Src
[0].Register
.File
))
401 info
->uses_bindless_images
= true;
403 case TGSI_OPCODE_STORE
:
404 if (tgsi_is_bindless_image_file(fullinst
->Dst
[0].Register
.File
))
405 info
->uses_bindless_images
= true;
411 if (fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_CENTROID
||
412 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
413 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
414 const struct tgsi_full_src_register
*src0
= &fullinst
->Src
[0];
417 is_interp_instruction
= true;
419 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
420 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
422 input
= src0
->Register
.Index
;
424 /* For the INTERP opcodes, the interpolation is always
425 * PERSPECTIVE unless LINEAR is specified.
427 switch (info
->input_interpolate
[input
]) {
428 case TGSI_INTERPOLATE_COLOR
:
429 case TGSI_INTERPOLATE_CONSTANT
:
430 case TGSI_INTERPOLATE_PERSPECTIVE
:
431 switch (fullinst
->Instruction
.Opcode
) {
432 case TGSI_OPCODE_INTERP_CENTROID
:
433 info
->uses_persp_opcode_interp_centroid
= TRUE
;
435 case TGSI_OPCODE_INTERP_OFFSET
:
436 info
->uses_persp_opcode_interp_offset
= TRUE
;
438 case TGSI_OPCODE_INTERP_SAMPLE
:
439 info
->uses_persp_opcode_interp_sample
= TRUE
;
444 case TGSI_INTERPOLATE_LINEAR
:
445 switch (fullinst
->Instruction
.Opcode
) {
446 case TGSI_OPCODE_INTERP_CENTROID
:
447 info
->uses_linear_opcode_interp_centroid
= TRUE
;
449 case TGSI_OPCODE_INTERP_OFFSET
:
450 info
->uses_linear_opcode_interp_offset
= TRUE
;
452 case TGSI_OPCODE_INTERP_SAMPLE
:
453 info
->uses_linear_opcode_interp_sample
= TRUE
;
460 if ((fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
461 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
) ||
462 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_DFMA
||
463 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_DDIV
||
464 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_D2U64
||
465 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_D2I64
||
466 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_U642D
||
467 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_I642D
)
468 info
->uses_doubles
= TRUE
;
470 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
471 scan_src_operand(info
, fullinst
, &fullinst
->Src
[i
], i
,
472 tgsi_util_get_inst_usage_mask(fullinst
, i
),
473 is_interp_instruction
, &is_mem_inst
);
476 if (fullinst
->Instruction
.Texture
) {
477 for (i
= 0; i
< fullinst
->Texture
.NumOffsets
; i
++) {
478 struct tgsi_full_src_register src
= {{0}};
480 src
.Register
.File
= fullinst
->TexOffsets
[i
].File
;
481 src
.Register
.Index
= fullinst
->TexOffsets
[i
].Index
;
482 src
.Register
.SwizzleX
= fullinst
->TexOffsets
[i
].SwizzleX
;
483 src
.Register
.SwizzleY
= fullinst
->TexOffsets
[i
].SwizzleY
;
484 src
.Register
.SwizzleZ
= fullinst
->TexOffsets
[i
].SwizzleZ
;
486 /* The usage mask is suboptimal but should be safe. */
487 scan_src_operand(info
, fullinst
, &src
, 0, TGSI_WRITEMASK_XYZ
,
488 false, &is_mem_inst
);
492 /* check for indirect register writes */
493 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
494 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
495 if (dst
->Register
.Indirect
) {
496 info
->indirect_files
|= (1 << dst
->Register
.File
);
497 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
500 if (dst
->Register
.Dimension
&& dst
->Dimension
.Indirect
)
501 info
->dim_indirect_files
|= 1u << dst
->Register
.File
;
503 if (is_memory_file(dst
->Register
.File
)) {
504 assert(fullinst
->Instruction
.Opcode
== TGSI_OPCODE_STORE
);
507 info
->writes_memory
= TRUE
;
509 if (dst
->Register
.File
== TGSI_FILE_IMAGE
) {
510 if (dst
->Register
.Indirect
)
511 info
->images_store
= info
->images_declared
;
513 info
->images_store
|= 1 << dst
->Register
.Index
;
514 } else if (dst
->Register
.File
== TGSI_FILE_BUFFER
) {
515 if (dst
->Register
.Indirect
)
516 info
->shader_buffers_store
= info
->shader_buffers_declared
;
518 info
->shader_buffers_store
|= 1 << dst
->Register
.Index
;
524 info
->num_memory_instructions
++;
526 if (computes_derivative(fullinst
->Instruction
.Opcode
))
527 info
->uses_derivatives
= true;
529 info
->num_instructions
++;
534 scan_declaration(struct tgsi_shader_info
*info
,
535 const struct tgsi_full_declaration
*fulldecl
)
537 const uint file
= fulldecl
->Declaration
.File
;
538 const unsigned procType
= info
->processor
;
541 if (fulldecl
->Declaration
.Array
) {
542 unsigned array_id
= fulldecl
->Array
.ArrayID
;
545 case TGSI_FILE_INPUT
:
546 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
547 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
548 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
550 case TGSI_FILE_OUTPUT
:
551 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
552 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
553 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
556 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
559 for (reg
= fulldecl
->Range
.First
; reg
<= fulldecl
->Range
.Last
; reg
++) {
560 unsigned semName
= fulldecl
->Semantic
.Name
;
561 unsigned semIndex
= fulldecl
->Semantic
.Index
+
562 (reg
- fulldecl
->Range
.First
);
564 unsigned index
, target
, type
;
566 /* only first 32 regs will appear in this bitfield */
567 info
->file_mask
[file
] |= (1 << reg
);
568 info
->file_count
[file
]++;
569 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
572 case TGSI_FILE_CONSTANT
:
575 if (fulldecl
->Declaration
.Dimension
)
576 buffer
= fulldecl
->Dim
.Index2D
;
578 info
->const_file_max
[buffer
] =
579 MAX2(info
->const_file_max
[buffer
], (int)reg
);
580 info
->const_buffers_declared
|= 1u << buffer
;
583 case TGSI_FILE_IMAGE
:
584 info
->images_declared
|= 1u << reg
;
585 if (fulldecl
->Image
.Resource
== TGSI_TEXTURE_BUFFER
)
586 info
->images_buffers
|= 1 << reg
;
589 case TGSI_FILE_BUFFER
:
590 info
->shader_buffers_declared
|= 1u << reg
;
593 case TGSI_FILE_INPUT
:
594 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
595 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
596 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
597 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
598 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
600 /* Vertex shaders can have inputs with holes between them. */
601 info
->num_inputs
= MAX2(info
->num_inputs
, reg
+ 1);
604 case TGSI_SEMANTIC_PRIMID
:
605 info
->uses_primid
= true;
607 case TGSI_SEMANTIC_POSITION
:
608 info
->reads_position
= true;
610 case TGSI_SEMANTIC_FACE
:
611 info
->uses_frontface
= true;
616 case TGSI_FILE_SYSTEM_VALUE
:
617 index
= fulldecl
->Range
.First
;
619 info
->system_value_semantic_name
[index
] = semName
;
620 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
623 case TGSI_SEMANTIC_INSTANCEID
:
624 info
->uses_instanceid
= TRUE
;
626 case TGSI_SEMANTIC_VERTEXID
:
627 info
->uses_vertexid
= TRUE
;
629 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
630 info
->uses_vertexid_nobase
= TRUE
;
632 case TGSI_SEMANTIC_BASEVERTEX
:
633 info
->uses_basevertex
= TRUE
;
635 case TGSI_SEMANTIC_PRIMID
:
636 info
->uses_primid
= TRUE
;
638 case TGSI_SEMANTIC_INVOCATIONID
:
639 info
->uses_invocationid
= TRUE
;
641 case TGSI_SEMANTIC_POSITION
:
642 info
->reads_position
= TRUE
;
644 case TGSI_SEMANTIC_FACE
:
645 info
->uses_frontface
= TRUE
;
647 case TGSI_SEMANTIC_SAMPLEMASK
:
648 info
->reads_samplemask
= TRUE
;
650 case TGSI_SEMANTIC_TESSINNER
:
651 case TGSI_SEMANTIC_TESSOUTER
:
652 info
->reads_tess_factors
= true;
657 case TGSI_FILE_OUTPUT
:
658 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
659 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
660 info
->output_usagemask
[reg
] |= fulldecl
->Declaration
.UsageMask
;
661 info
->num_outputs
= MAX2(info
->num_outputs
, reg
+ 1);
663 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_X
) {
664 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamX
;
665 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamX
]++;
667 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Y
) {
668 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamY
<< 2;
669 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamY
]++;
671 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Z
) {
672 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamZ
<< 4;
673 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamZ
]++;
675 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_W
) {
676 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamW
<< 6;
677 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamW
]++;
681 case TGSI_SEMANTIC_PRIMID
:
682 info
->writes_primid
= true;
684 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
685 info
->writes_viewport_index
= true;
687 case TGSI_SEMANTIC_LAYER
:
688 info
->writes_layer
= true;
690 case TGSI_SEMANTIC_PSIZE
:
691 info
->writes_psize
= true;
693 case TGSI_SEMANTIC_CLIPVERTEX
:
694 info
->writes_clipvertex
= true;
696 case TGSI_SEMANTIC_COLOR
:
697 info
->colors_written
|= 1 << semIndex
;
699 case TGSI_SEMANTIC_STENCIL
:
700 info
->writes_stencil
= true;
702 case TGSI_SEMANTIC_SAMPLEMASK
:
703 info
->writes_samplemask
= true;
705 case TGSI_SEMANTIC_EDGEFLAG
:
706 info
->writes_edgeflag
= true;
708 case TGSI_SEMANTIC_POSITION
:
709 if (procType
== PIPE_SHADER_FRAGMENT
)
710 info
->writes_z
= true;
712 info
->writes_position
= true;
717 case TGSI_FILE_SAMPLER
:
718 STATIC_ASSERT(sizeof(info
->samplers_declared
) * 8 >= PIPE_MAX_SAMPLERS
);
719 info
->samplers_declared
|= 1u << reg
;
722 case TGSI_FILE_SAMPLER_VIEW
:
723 target
= fulldecl
->SamplerView
.Resource
;
724 type
= fulldecl
->SamplerView
.ReturnTypeX
;
726 assert(target
< TGSI_TEXTURE_UNKNOWN
);
727 if (info
->sampler_targets
[reg
] == TGSI_TEXTURE_UNKNOWN
) {
728 /* Save sampler target for this sampler index */
729 info
->sampler_targets
[reg
] = target
;
730 info
->sampler_type
[reg
] = type
;
732 /* if previously declared, make sure targets agree */
733 assert(info
->sampler_targets
[reg
] == target
);
734 assert(info
->sampler_type
[reg
] == type
);
743 scan_immediate(struct tgsi_shader_info
*info
)
745 uint reg
= info
->immediate_count
++;
746 uint file
= TGSI_FILE_IMMEDIATE
;
748 info
->file_mask
[file
] |= (1 << reg
);
749 info
->file_count
[file
]++;
750 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
755 scan_property(struct tgsi_shader_info
*info
,
756 const struct tgsi_full_property
*fullprop
)
758 unsigned name
= fullprop
->Property
.PropertyName
;
759 unsigned value
= fullprop
->u
[0].Data
;
761 assert(name
< ARRAY_SIZE(info
->properties
));
762 info
->properties
[name
] = value
;
765 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
766 info
->num_written_clipdistance
= value
;
767 info
->clipdist_writemask
|= (1 << value
) - 1;
769 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
770 info
->num_written_culldistance
= value
;
771 info
->culldist_writemask
|= (1 << value
) - 1;
778 * Scan the given TGSI shader to collect information such as number of
779 * registers used, special instructions used, etc.
780 * \return info the result of the scan
783 tgsi_scan_shader(const struct tgsi_token
*tokens
,
784 struct tgsi_shader_info
*info
)
787 struct tgsi_parse_context parse
;
788 unsigned current_depth
= 0;
790 memset(info
, 0, sizeof(*info
));
791 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
792 info
->file_max
[i
] = -1;
793 for (i
= 0; i
< ARRAY_SIZE(info
->const_file_max
); i
++)
794 info
->const_file_max
[i
] = -1;
795 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
796 for (i
= 0; i
< ARRAY_SIZE(info
->sampler_targets
); i
++)
797 info
->sampler_targets
[i
] = TGSI_TEXTURE_UNKNOWN
;
800 ** Setup to begin parsing input shader
802 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
803 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
806 procType
= parse
.FullHeader
.Processor
.Processor
;
807 assert(procType
== PIPE_SHADER_FRAGMENT
||
808 procType
== PIPE_SHADER_VERTEX
||
809 procType
== PIPE_SHADER_GEOMETRY
||
810 procType
== PIPE_SHADER_TESS_CTRL
||
811 procType
== PIPE_SHADER_TESS_EVAL
||
812 procType
== PIPE_SHADER_COMPUTE
);
813 info
->processor
= procType
;
816 ** Loop over incoming program tokens/instructions
818 while (!tgsi_parse_end_of_tokens(&parse
)) {
821 tgsi_parse_token( &parse
);
823 switch( parse
.FullToken
.Token
.Type
) {
824 case TGSI_TOKEN_TYPE_INSTRUCTION
:
825 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
828 case TGSI_TOKEN_TYPE_DECLARATION
:
829 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
831 case TGSI_TOKEN_TYPE_IMMEDIATE
:
832 scan_immediate(info
);
834 case TGSI_TOKEN_TYPE_PROPERTY
:
835 scan_property(info
, &parse
.FullToken
.FullProperty
);
838 assert(!"Unexpected TGSI token type");
842 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
843 info
->opcode_count
[TGSI_OPCODE_KILL
]);
845 /* The dimensions of the IN decleration in geometry shader have
846 * to be deduced from the type of the input primitive.
848 if (procType
== PIPE_SHADER_GEOMETRY
) {
849 unsigned input_primitive
=
850 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
851 int num_verts
= u_vertices_per_prim(input_primitive
);
853 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
854 info
->file_max
[TGSI_FILE_INPUT
] =
855 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
856 for (j
= 0; j
< num_verts
; ++j
) {
857 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
861 tgsi_parse_free(&parse
);
865 * Collect information about the arrays of a given register file.
867 * @param tokens TGSI shader
868 * @param file the register file to scan through
869 * @param max_array_id number of entries in @p arrays; should be equal to the
870 * highest array id, i.e. tgsi_shader_info::array_max[file].
871 * @param arrays info for array of each ID will be written to arrays[ID - 1].
874 tgsi_scan_arrays(const struct tgsi_token
*tokens
,
876 unsigned max_array_id
,
877 struct tgsi_array_info
*arrays
)
879 struct tgsi_parse_context parse
;
881 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
882 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
886 memset(arrays
, 0, sizeof(arrays
[0]) * max_array_id
);
888 while (!tgsi_parse_end_of_tokens(&parse
)) {
889 struct tgsi_full_instruction
*inst
;
891 tgsi_parse_token(&parse
);
893 if (parse
.FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
894 struct tgsi_full_declaration
*decl
= &parse
.FullToken
.FullDeclaration
;
896 if (decl
->Declaration
.Array
&& decl
->Declaration
.File
== file
&&
897 decl
->Array
.ArrayID
> 0 && decl
->Array
.ArrayID
<= max_array_id
) {
898 struct tgsi_array_info
*array
= &arrays
[decl
->Array
.ArrayID
- 1];
899 assert(!array
->declared
);
900 array
->declared
= true;
901 array
->range
= decl
->Range
;
905 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
908 inst
= &parse
.FullToken
.FullInstruction
;
909 for (unsigned i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
910 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
911 if (dst
->Register
.File
!= file
)
914 if (dst
->Register
.Indirect
) {
915 if (dst
->Indirect
.ArrayID
> 0 &&
916 dst
->Indirect
.ArrayID
<= max_array_id
) {
917 arrays
[dst
->Indirect
.ArrayID
- 1].writemask
|= dst
->Register
.WriteMask
;
919 /* Indirect writes without an ArrayID can write anywhere. */
920 for (unsigned j
= 0; j
< max_array_id
; ++j
)
921 arrays
[j
].writemask
|= dst
->Register
.WriteMask
;
924 /* Check whether the write falls into any of the arrays anyway. */
925 for (unsigned j
= 0; j
< max_array_id
; ++j
) {
926 struct tgsi_array_info
*array
= &arrays
[j
];
927 if (array
->declared
&&
928 dst
->Register
.Index
>= array
->range
.First
&&
929 dst
->Register
.Index
<= array
->range
.Last
)
930 array
->writemask
|= dst
->Register
.WriteMask
;
936 tgsi_parse_free(&parse
);