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
,
110 unsigned usage_mask_after_swizzle
,
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
) {
120 name
= info
->system_value_semantic_name
[src
->Register
.Index
];
123 case TGSI_SEMANTIC_THREAD_ID
:
124 case TGSI_SEMANTIC_BLOCK_ID
:
125 mask
= usage_mask_after_swizzle
& TGSI_WRITEMASK_XYZ
;
127 unsigned i
= u_bit_scan(&mask
);
129 if (name
== TGSI_SEMANTIC_THREAD_ID
)
130 info
->uses_thread_id
[i
] = true;
132 info
->uses_block_id
[i
] = true;
135 case TGSI_SEMANTIC_BLOCK_SIZE
:
136 /* The block size is translated to IMM with a fixed block size. */
137 if (info
->properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0)
138 info
->uses_block_size
= true;
140 case TGSI_SEMANTIC_GRID_SIZE
:
141 info
->uses_grid_size
= true;
146 /* Mark which inputs are effectively used */
147 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
148 if (src
->Register
.Indirect
) {
149 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
150 info
->input_usage_mask
[ind
] |= usage_mask_after_swizzle
;
154 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
155 info
->input_usage_mask
[ind
] |= usage_mask_after_swizzle
;
158 if (info
->processor
== PIPE_SHADER_FRAGMENT
) {
159 unsigned name
, index
, input
;
161 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
162 input
= info
->input_array_first
[src
->Indirect
.ArrayID
];
164 input
= src
->Register
.Index
;
166 name
= info
->input_semantic_name
[input
];
167 index
= info
->input_semantic_index
[input
];
169 if (name
== TGSI_SEMANTIC_POSITION
&&
170 usage_mask_after_swizzle
& TGSI_WRITEMASK_Z
)
171 info
->reads_z
= true;
173 if (name
== TGSI_SEMANTIC_COLOR
)
174 info
->colors_read
|= usage_mask_after_swizzle
<< (index
* 4);
176 /* Process only interpolated varyings. Don't include POSITION.
177 * Don't include integer varyings, because they are not
178 * interpolated. Don't process inputs interpolated by INTERP
179 * opcodes. Those are tracked separately.
181 if ((!is_interp_instruction
|| src_index
!= 0) &&
182 (name
== TGSI_SEMANTIC_GENERIC
||
183 name
== TGSI_SEMANTIC_TEXCOORD
||
184 name
== TGSI_SEMANTIC_COLOR
||
185 name
== TGSI_SEMANTIC_BCOLOR
||
186 name
== TGSI_SEMANTIC_FOG
||
187 name
== TGSI_SEMANTIC_CLIPDIST
)) {
188 switch (info
->input_interpolate
[input
]) {
189 case TGSI_INTERPOLATE_COLOR
:
190 case TGSI_INTERPOLATE_PERSPECTIVE
:
191 switch (info
->input_interpolate_loc
[input
]) {
192 case TGSI_INTERPOLATE_LOC_CENTER
:
193 info
->uses_persp_center
= TRUE
;
195 case TGSI_INTERPOLATE_LOC_CENTROID
:
196 info
->uses_persp_centroid
= TRUE
;
198 case TGSI_INTERPOLATE_LOC_SAMPLE
:
199 info
->uses_persp_sample
= TRUE
;
203 case TGSI_INTERPOLATE_LINEAR
:
204 switch (info
->input_interpolate_loc
[input
]) {
205 case TGSI_INTERPOLATE_LOC_CENTER
:
206 info
->uses_linear_center
= TRUE
;
208 case TGSI_INTERPOLATE_LOC_CENTROID
:
209 info
->uses_linear_centroid
= TRUE
;
211 case TGSI_INTERPOLATE_LOC_SAMPLE
:
212 info
->uses_linear_sample
= TRUE
;
216 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
222 if (info
->processor
== PIPE_SHADER_TESS_CTRL
&&
223 src
->Register
.File
== TGSI_FILE_OUTPUT
) {
226 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
227 input
= info
->output_array_first
[src
->Indirect
.ArrayID
];
229 input
= src
->Register
.Index
;
231 switch (info
->output_semantic_name
[input
]) {
232 case TGSI_SEMANTIC_PATCH
:
233 info
->reads_perpatch_outputs
= true;
235 case TGSI_SEMANTIC_TESSINNER
:
236 case TGSI_SEMANTIC_TESSOUTER
:
237 info
->reads_tessfactor_outputs
= true;
240 info
->reads_pervertex_outputs
= true;
244 /* check for indirect register reads */
245 if (src
->Register
.Indirect
) {
246 info
->indirect_files
|= (1 << src
->Register
.File
);
247 info
->indirect_files_read
|= (1 << src
->Register
.File
);
249 /* record indirect constant buffer indexing */
250 if (src
->Register
.File
== TGSI_FILE_CONSTANT
) {
251 if (src
->Register
.Dimension
) {
252 if (src
->Dimension
.Indirect
)
253 info
->const_buffers_indirect
= info
->const_buffers_declared
;
255 info
->const_buffers_indirect
|= 1u << src
->Dimension
.Index
;
257 info
->const_buffers_indirect
|= 1;
262 if (src
->Register
.Dimension
&& src
->Dimension
.Indirect
)
263 info
->dim_indirect_files
|= 1u << src
->Register
.File
;
265 /* Texture samplers */
266 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
267 const unsigned index
= src
->Register
.Index
;
269 assert(fullinst
->Instruction
.Texture
);
270 assert(index
< ARRAY_SIZE(info
->is_msaa_sampler
));
271 assert(index
< PIPE_MAX_SAMPLERS
);
273 if (is_texture_inst(fullinst
->Instruction
.Opcode
)) {
274 const unsigned target
= fullinst
->Texture
.Texture
;
275 assert(target
< TGSI_TEXTURE_UNKNOWN
);
276 /* for texture instructions, check that the texture instruction
277 * target matches the previous sampler view declaration (if there
280 if (info
->sampler_targets
[index
] == TGSI_TEXTURE_UNKNOWN
) {
281 /* probably no sampler view declaration */
282 info
->sampler_targets
[index
] = target
;
284 /* Make sure the texture instruction's sampler/target info
285 * agrees with the sampler view declaration.
287 assert(info
->sampler_targets
[index
] == target
);
290 if (target
== TGSI_TEXTURE_2D_MSAA
||
291 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
292 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
297 if (is_memory_file(src
->Register
.File
) &&
298 !is_mem_query_inst(fullinst
->Instruction
.Opcode
)) {
301 if (tgsi_get_opcode_info(fullinst
->Instruction
.Opcode
)->is_store
) {
302 info
->writes_memory
= TRUE
;
304 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
305 if (src
->Register
.Indirect
)
306 info
->images_atomic
= info
->images_declared
;
308 info
->images_atomic
|= 1 << src
->Register
.Index
;
309 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
310 if (src
->Register
.Indirect
)
311 info
->shader_buffers_atomic
= info
->shader_buffers_declared
;
313 info
->shader_buffers_atomic
|= 1 << src
->Register
.Index
;
316 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
317 if (src
->Register
.Indirect
)
318 info
->images_load
= info
->images_declared
;
320 info
->images_load
|= 1 << src
->Register
.Index
;
321 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
322 if (src
->Register
.Indirect
)
323 info
->shader_buffers_load
= info
->shader_buffers_declared
;
325 info
->shader_buffers_load
|= 1 << src
->Register
.Index
;
333 scan_instruction(struct tgsi_shader_info
*info
,
334 const struct tgsi_full_instruction
*fullinst
,
335 unsigned *current_depth
)
338 bool is_mem_inst
= false;
339 bool is_interp_instruction
= false;
340 unsigned sampler_src
;
342 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
343 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
345 switch (fullinst
->Instruction
.Opcode
) {
347 case TGSI_OPCODE_UIF
:
348 case TGSI_OPCODE_BGNLOOP
:
350 info
->max_depth
= MAX2(info
->max_depth
, *current_depth
);
352 case TGSI_OPCODE_ENDIF
:
353 case TGSI_OPCODE_ENDLOOP
:
356 case TGSI_OPCODE_TEX
:
357 case TGSI_OPCODE_TEX_LZ
:
358 case TGSI_OPCODE_TXB
:
359 case TGSI_OPCODE_TXD
:
360 case TGSI_OPCODE_TXL
:
361 case TGSI_OPCODE_TXP
:
362 case TGSI_OPCODE_TXQ
:
363 case TGSI_OPCODE_TXQS
:
364 case TGSI_OPCODE_TXF
:
365 case TGSI_OPCODE_TXF_LZ
:
366 case TGSI_OPCODE_TEX2
:
367 case TGSI_OPCODE_TXB2
:
368 case TGSI_OPCODE_TXL2
:
369 case TGSI_OPCODE_TG4
:
370 case TGSI_OPCODE_LODQ
:
371 sampler_src
= fullinst
->Instruction
.NumSrcRegs
- 1;
372 if (fullinst
->Src
[sampler_src
].Register
.File
!= TGSI_FILE_SAMPLER
)
373 info
->uses_bindless_samplers
= true;
375 case TGSI_OPCODE_RESQ
:
376 case TGSI_OPCODE_LOAD
:
377 case TGSI_OPCODE_ATOMUADD
:
378 case TGSI_OPCODE_ATOMXCHG
:
379 case TGSI_OPCODE_ATOMCAS
:
380 case TGSI_OPCODE_ATOMAND
:
381 case TGSI_OPCODE_ATOMOR
:
382 case TGSI_OPCODE_ATOMXOR
:
383 case TGSI_OPCODE_ATOMUMIN
:
384 case TGSI_OPCODE_ATOMUMAX
:
385 case TGSI_OPCODE_ATOMIMIN
:
386 case TGSI_OPCODE_ATOMIMAX
:
387 if (tgsi_is_bindless_image_file(fullinst
->Src
[0].Register
.File
))
388 info
->uses_bindless_images
= true;
390 case TGSI_OPCODE_STORE
:
391 if (tgsi_is_bindless_image_file(fullinst
->Dst
[0].Register
.File
))
392 info
->uses_bindless_images
= true;
398 if (fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_CENTROID
||
399 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
400 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
401 const struct tgsi_full_src_register
*src0
= &fullinst
->Src
[0];
404 is_interp_instruction
= true;
406 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
407 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
409 input
= src0
->Register
.Index
;
411 /* For the INTERP opcodes, the interpolation is always
412 * PERSPECTIVE unless LINEAR is specified.
414 switch (info
->input_interpolate
[input
]) {
415 case TGSI_INTERPOLATE_COLOR
:
416 case TGSI_INTERPOLATE_CONSTANT
:
417 case TGSI_INTERPOLATE_PERSPECTIVE
:
418 switch (fullinst
->Instruction
.Opcode
) {
419 case TGSI_OPCODE_INTERP_CENTROID
:
420 info
->uses_persp_opcode_interp_centroid
= TRUE
;
422 case TGSI_OPCODE_INTERP_OFFSET
:
423 info
->uses_persp_opcode_interp_offset
= TRUE
;
425 case TGSI_OPCODE_INTERP_SAMPLE
:
426 info
->uses_persp_opcode_interp_sample
= TRUE
;
431 case TGSI_INTERPOLATE_LINEAR
:
432 switch (fullinst
->Instruction
.Opcode
) {
433 case TGSI_OPCODE_INTERP_CENTROID
:
434 info
->uses_linear_opcode_interp_centroid
= TRUE
;
436 case TGSI_OPCODE_INTERP_OFFSET
:
437 info
->uses_linear_opcode_interp_offset
= TRUE
;
439 case TGSI_OPCODE_INTERP_SAMPLE
:
440 info
->uses_linear_opcode_interp_sample
= TRUE
;
447 if ((fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
448 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
) ||
449 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_DFMA
||
450 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_DDIV
||
451 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_D2U64
||
452 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_D2I64
||
453 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_U642D
||
454 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_I642D
)
455 info
->uses_doubles
= TRUE
;
457 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
458 scan_src_operand(info
, fullinst
, &fullinst
->Src
[i
], i
,
459 tgsi_util_get_inst_usage_mask(fullinst
, i
),
460 is_interp_instruction
, &is_mem_inst
);
463 if (fullinst
->Instruction
.Texture
) {
464 for (i
= 0; i
< fullinst
->Texture
.NumOffsets
; i
++) {
465 struct tgsi_full_src_register src
= {{0}};
467 src
.Register
.File
= fullinst
->TexOffsets
[i
].File
;
468 src
.Register
.Index
= fullinst
->TexOffsets
[i
].Index
;
469 src
.Register
.SwizzleX
= fullinst
->TexOffsets
[i
].SwizzleX
;
470 src
.Register
.SwizzleY
= fullinst
->TexOffsets
[i
].SwizzleY
;
471 src
.Register
.SwizzleZ
= fullinst
->TexOffsets
[i
].SwizzleZ
;
473 /* The usage mask is suboptimal but should be safe. */
474 scan_src_operand(info
, fullinst
, &src
, -1, TGSI_WRITEMASK_XYZ
,
475 false, &is_mem_inst
);
479 /* check for indirect register writes */
480 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
481 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
482 if (dst
->Register
.Indirect
) {
483 info
->indirect_files
|= (1 << dst
->Register
.File
);
484 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
487 if (dst
->Register
.Dimension
&& dst
->Dimension
.Indirect
)
488 info
->dim_indirect_files
|= 1u << dst
->Register
.File
;
490 if (is_memory_file(dst
->Register
.File
)) {
491 assert(fullinst
->Instruction
.Opcode
== TGSI_OPCODE_STORE
);
494 info
->writes_memory
= TRUE
;
496 if (dst
->Register
.File
== TGSI_FILE_IMAGE
) {
497 if (dst
->Register
.Indirect
)
498 info
->images_store
= info
->images_declared
;
500 info
->images_store
|= 1 << dst
->Register
.Index
;
501 } else if (dst
->Register
.File
== TGSI_FILE_BUFFER
) {
502 if (dst
->Register
.Indirect
)
503 info
->shader_buffers_store
= info
->shader_buffers_declared
;
505 info
->shader_buffers_store
|= 1 << dst
->Register
.Index
;
511 info
->num_memory_instructions
++;
513 if (computes_derivative(fullinst
->Instruction
.Opcode
))
514 info
->uses_derivatives
= true;
516 info
->num_instructions
++;
521 scan_declaration(struct tgsi_shader_info
*info
,
522 const struct tgsi_full_declaration
*fulldecl
)
524 const uint file
= fulldecl
->Declaration
.File
;
525 const unsigned procType
= info
->processor
;
528 if (fulldecl
->Declaration
.Array
) {
529 unsigned array_id
= fulldecl
->Array
.ArrayID
;
532 case TGSI_FILE_INPUT
:
533 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
534 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
535 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
537 case TGSI_FILE_OUTPUT
:
538 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
539 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
540 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
543 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
546 for (reg
= fulldecl
->Range
.First
; reg
<= fulldecl
->Range
.Last
; reg
++) {
547 unsigned semName
= fulldecl
->Semantic
.Name
;
548 unsigned semIndex
= fulldecl
->Semantic
.Index
+
549 (reg
- fulldecl
->Range
.First
);
551 unsigned index
, target
, type
;
553 /* only first 32 regs will appear in this bitfield */
554 info
->file_mask
[file
] |= (1 << reg
);
555 info
->file_count
[file
]++;
556 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
559 case TGSI_FILE_CONSTANT
:
562 if (fulldecl
->Declaration
.Dimension
)
563 buffer
= fulldecl
->Dim
.Index2D
;
565 info
->const_file_max
[buffer
] =
566 MAX2(info
->const_file_max
[buffer
], (int)reg
);
567 info
->const_buffers_declared
|= 1u << buffer
;
570 case TGSI_FILE_IMAGE
:
571 info
->images_declared
|= 1u << reg
;
572 if (fulldecl
->Image
.Resource
== TGSI_TEXTURE_BUFFER
)
573 info
->images_buffers
|= 1 << reg
;
576 case TGSI_FILE_BUFFER
:
577 info
->shader_buffers_declared
|= 1u << reg
;
580 case TGSI_FILE_INPUT
:
581 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
582 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
583 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
584 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
585 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
587 /* Vertex shaders can have inputs with holes between them. */
588 info
->num_inputs
= MAX2(info
->num_inputs
, reg
+ 1);
591 case TGSI_SEMANTIC_PRIMID
:
592 info
->uses_primid
= true;
594 case TGSI_SEMANTIC_POSITION
:
595 info
->reads_position
= true;
597 case TGSI_SEMANTIC_FACE
:
598 info
->uses_frontface
= true;
603 case TGSI_FILE_SYSTEM_VALUE
:
604 index
= fulldecl
->Range
.First
;
606 info
->system_value_semantic_name
[index
] = semName
;
607 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
610 case TGSI_SEMANTIC_INSTANCEID
:
611 info
->uses_instanceid
= TRUE
;
613 case TGSI_SEMANTIC_VERTEXID
:
614 info
->uses_vertexid
= TRUE
;
616 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
617 info
->uses_vertexid_nobase
= TRUE
;
619 case TGSI_SEMANTIC_BASEVERTEX
:
620 info
->uses_basevertex
= TRUE
;
622 case TGSI_SEMANTIC_PRIMID
:
623 info
->uses_primid
= TRUE
;
625 case TGSI_SEMANTIC_INVOCATIONID
:
626 info
->uses_invocationid
= TRUE
;
628 case TGSI_SEMANTIC_POSITION
:
629 info
->reads_position
= TRUE
;
631 case TGSI_SEMANTIC_FACE
:
632 info
->uses_frontface
= TRUE
;
634 case TGSI_SEMANTIC_SAMPLEMASK
:
635 info
->reads_samplemask
= TRUE
;
637 case TGSI_SEMANTIC_TESSINNER
:
638 case TGSI_SEMANTIC_TESSOUTER
:
639 info
->reads_tess_factors
= true;
644 case TGSI_FILE_OUTPUT
:
645 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
646 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
647 info
->output_usagemask
[reg
] |= fulldecl
->Declaration
.UsageMask
;
648 info
->num_outputs
= MAX2(info
->num_outputs
, reg
+ 1);
650 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_X
) {
651 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamX
;
652 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamX
]++;
654 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Y
) {
655 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamY
<< 2;
656 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamY
]++;
658 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Z
) {
659 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamZ
<< 4;
660 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamZ
]++;
662 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_W
) {
663 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamW
<< 6;
664 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamW
]++;
668 case TGSI_SEMANTIC_PRIMID
:
669 info
->writes_primid
= true;
671 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
672 info
->writes_viewport_index
= true;
674 case TGSI_SEMANTIC_LAYER
:
675 info
->writes_layer
= true;
677 case TGSI_SEMANTIC_PSIZE
:
678 info
->writes_psize
= true;
680 case TGSI_SEMANTIC_CLIPVERTEX
:
681 info
->writes_clipvertex
= true;
683 case TGSI_SEMANTIC_COLOR
:
684 info
->colors_written
|= 1 << semIndex
;
686 case TGSI_SEMANTIC_STENCIL
:
687 info
->writes_stencil
= true;
689 case TGSI_SEMANTIC_SAMPLEMASK
:
690 info
->writes_samplemask
= true;
692 case TGSI_SEMANTIC_EDGEFLAG
:
693 info
->writes_edgeflag
= true;
695 case TGSI_SEMANTIC_POSITION
:
696 if (procType
== PIPE_SHADER_FRAGMENT
)
697 info
->writes_z
= true;
699 info
->writes_position
= true;
704 case TGSI_FILE_SAMPLER
:
705 STATIC_ASSERT(sizeof(info
->samplers_declared
) * 8 >= PIPE_MAX_SAMPLERS
);
706 info
->samplers_declared
|= 1u << reg
;
709 case TGSI_FILE_SAMPLER_VIEW
:
710 target
= fulldecl
->SamplerView
.Resource
;
711 type
= fulldecl
->SamplerView
.ReturnTypeX
;
713 assert(target
< TGSI_TEXTURE_UNKNOWN
);
714 if (info
->sampler_targets
[reg
] == TGSI_TEXTURE_UNKNOWN
) {
715 /* Save sampler target for this sampler index */
716 info
->sampler_targets
[reg
] = target
;
717 info
->sampler_type
[reg
] = type
;
719 /* if previously declared, make sure targets agree */
720 assert(info
->sampler_targets
[reg
] == target
);
721 assert(info
->sampler_type
[reg
] == type
);
730 scan_immediate(struct tgsi_shader_info
*info
)
732 uint reg
= info
->immediate_count
++;
733 uint file
= TGSI_FILE_IMMEDIATE
;
735 info
->file_mask
[file
] |= (1 << reg
);
736 info
->file_count
[file
]++;
737 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
742 scan_property(struct tgsi_shader_info
*info
,
743 const struct tgsi_full_property
*fullprop
)
745 unsigned name
= fullprop
->Property
.PropertyName
;
746 unsigned value
= fullprop
->u
[0].Data
;
748 assert(name
< ARRAY_SIZE(info
->properties
));
749 info
->properties
[name
] = value
;
752 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
753 info
->num_written_clipdistance
= value
;
754 info
->clipdist_writemask
|= (1 << value
) - 1;
756 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
757 info
->num_written_culldistance
= value
;
758 info
->culldist_writemask
|= (1 << value
) - 1;
765 * Scan the given TGSI shader to collect information such as number of
766 * registers used, special instructions used, etc.
767 * \return info the result of the scan
770 tgsi_scan_shader(const struct tgsi_token
*tokens
,
771 struct tgsi_shader_info
*info
)
774 struct tgsi_parse_context parse
;
775 unsigned current_depth
= 0;
777 memset(info
, 0, sizeof(*info
));
778 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
779 info
->file_max
[i
] = -1;
780 for (i
= 0; i
< ARRAY_SIZE(info
->const_file_max
); i
++)
781 info
->const_file_max
[i
] = -1;
782 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
783 for (i
= 0; i
< ARRAY_SIZE(info
->sampler_targets
); i
++)
784 info
->sampler_targets
[i
] = TGSI_TEXTURE_UNKNOWN
;
787 ** Setup to begin parsing input shader
789 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
790 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
793 procType
= parse
.FullHeader
.Processor
.Processor
;
794 assert(procType
== PIPE_SHADER_FRAGMENT
||
795 procType
== PIPE_SHADER_VERTEX
||
796 procType
== PIPE_SHADER_GEOMETRY
||
797 procType
== PIPE_SHADER_TESS_CTRL
||
798 procType
== PIPE_SHADER_TESS_EVAL
||
799 procType
== PIPE_SHADER_COMPUTE
);
800 info
->processor
= procType
;
803 ** Loop over incoming program tokens/instructions
805 while (!tgsi_parse_end_of_tokens(&parse
)) {
808 tgsi_parse_token( &parse
);
810 switch( parse
.FullToken
.Token
.Type
) {
811 case TGSI_TOKEN_TYPE_INSTRUCTION
:
812 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
815 case TGSI_TOKEN_TYPE_DECLARATION
:
816 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
818 case TGSI_TOKEN_TYPE_IMMEDIATE
:
819 scan_immediate(info
);
821 case TGSI_TOKEN_TYPE_PROPERTY
:
822 scan_property(info
, &parse
.FullToken
.FullProperty
);
825 assert(!"Unexpected TGSI token type");
829 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
830 info
->opcode_count
[TGSI_OPCODE_KILL
]);
832 /* The dimensions of the IN decleration in geometry shader have
833 * to be deduced from the type of the input primitive.
835 if (procType
== PIPE_SHADER_GEOMETRY
) {
836 unsigned input_primitive
=
837 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
838 int num_verts
= u_vertices_per_prim(input_primitive
);
840 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
841 info
->file_max
[TGSI_FILE_INPUT
] =
842 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
843 for (j
= 0; j
< num_verts
; ++j
) {
844 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
848 tgsi_parse_free(&parse
);
852 * Collect information about the arrays of a given register file.
854 * @param tokens TGSI shader
855 * @param file the register file to scan through
856 * @param max_array_id number of entries in @p arrays; should be equal to the
857 * highest array id, i.e. tgsi_shader_info::array_max[file].
858 * @param arrays info for array of each ID will be written to arrays[ID - 1].
861 tgsi_scan_arrays(const struct tgsi_token
*tokens
,
863 unsigned max_array_id
,
864 struct tgsi_array_info
*arrays
)
866 struct tgsi_parse_context parse
;
868 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
869 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
873 memset(arrays
, 0, sizeof(arrays
[0]) * max_array_id
);
875 while (!tgsi_parse_end_of_tokens(&parse
)) {
876 struct tgsi_full_instruction
*inst
;
878 tgsi_parse_token(&parse
);
880 if (parse
.FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
881 struct tgsi_full_declaration
*decl
= &parse
.FullToken
.FullDeclaration
;
883 if (decl
->Declaration
.Array
&& decl
->Declaration
.File
== file
&&
884 decl
->Array
.ArrayID
> 0 && decl
->Array
.ArrayID
<= max_array_id
) {
885 struct tgsi_array_info
*array
= &arrays
[decl
->Array
.ArrayID
- 1];
886 assert(!array
->declared
);
887 array
->declared
= true;
888 array
->range
= decl
->Range
;
892 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
895 inst
= &parse
.FullToken
.FullInstruction
;
896 for (unsigned i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
897 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
898 if (dst
->Register
.File
!= file
)
901 if (dst
->Register
.Indirect
) {
902 if (dst
->Indirect
.ArrayID
> 0 &&
903 dst
->Indirect
.ArrayID
<= max_array_id
) {
904 arrays
[dst
->Indirect
.ArrayID
- 1].writemask
|= dst
->Register
.WriteMask
;
906 /* Indirect writes without an ArrayID can write anywhere. */
907 for (unsigned j
= 0; j
< max_array_id
; ++j
)
908 arrays
[j
].writemask
|= dst
->Register
.WriteMask
;
911 /* Check whether the write falls into any of the arrays anyway. */
912 for (unsigned j
= 0; j
< max_array_id
; ++j
) {
913 struct tgsi_array_info
*array
= &arrays
[j
];
914 if (array
->declared
&&
915 dst
->Register
.Index
>= array
->range
.First
&&
916 dst
->Register
.Index
<= array
->range
.Last
)
917 array
->writemask
|= dst
->Register
.WriteMask
;
923 tgsi_parse_free(&parse
);
929 check_no_subroutines(const struct tgsi_full_instruction
*inst
)
931 switch (inst
->Instruction
.Opcode
) {
932 case TGSI_OPCODE_BGNSUB
:
933 case TGSI_OPCODE_ENDSUB
:
934 case TGSI_OPCODE_CAL
:
935 unreachable("subroutines unhandled");
940 get_inst_tessfactor_writemask(const struct tgsi_shader_info
*info
,
941 const struct tgsi_full_instruction
*inst
)
943 unsigned writemask
= 0;
945 for (unsigned i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
946 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
948 if (dst
->Register
.File
== TGSI_FILE_OUTPUT
&&
949 !dst
->Register
.Indirect
) {
950 unsigned name
= info
->output_semantic_name
[dst
->Register
.Index
];
952 if (name
== TGSI_SEMANTIC_TESSINNER
)
953 writemask
|= dst
->Register
.WriteMask
;
954 else if (name
== TGSI_SEMANTIC_TESSOUTER
)
955 writemask
|= dst
->Register
.WriteMask
<< 4;
962 get_block_tessfactor_writemask(const struct tgsi_shader_info
*info
,
963 struct tgsi_parse_context
*parse
,
966 struct tgsi_full_instruction
*inst
;
967 unsigned writemask
= 0;
970 tgsi_parse_token(parse
);
971 assert(parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
972 inst
= &parse
->FullToken
.FullInstruction
;
973 check_no_subroutines(inst
);
975 /* Recursively process nested blocks. */
976 switch (inst
->Instruction
.Opcode
) {
978 case TGSI_OPCODE_UIF
:
980 get_block_tessfactor_writemask(info
, parse
, TGSI_OPCODE_ENDIF
);
983 case TGSI_OPCODE_BGNLOOP
:
985 get_block_tessfactor_writemask(info
, parse
, TGSI_OPCODE_ENDLOOP
);
988 case TGSI_OPCODE_BARRIER
:
989 unreachable("nested BARRIER is illegal");
993 writemask
|= get_inst_tessfactor_writemask(info
, inst
);
994 } while (inst
->Instruction
.Opcode
!= end_opcode
);
1000 get_if_block_tessfactor_writemask(const struct tgsi_shader_info
*info
,
1001 struct tgsi_parse_context
*parse
,
1002 unsigned *upper_block_tf_writemask
,
1003 unsigned *cond_block_tf_writemask
)
1005 struct tgsi_full_instruction
*inst
;
1006 unsigned then_tessfactor_writemask
= 0;
1007 unsigned else_tessfactor_writemask
= 0;
1008 bool is_then
= true;
1011 tgsi_parse_token(parse
);
1012 assert(parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
1013 inst
= &parse
->FullToken
.FullInstruction
;
1014 check_no_subroutines(inst
);
1016 switch (inst
->Instruction
.Opcode
) {
1017 case TGSI_OPCODE_ELSE
:
1021 /* Recursively process nested blocks. */
1022 case TGSI_OPCODE_IF
:
1023 case TGSI_OPCODE_UIF
:
1024 get_if_block_tessfactor_writemask(info
, parse
,
1025 is_then
? &then_tessfactor_writemask
:
1026 &else_tessfactor_writemask
,
1027 cond_block_tf_writemask
);
1030 case TGSI_OPCODE_BGNLOOP
:
1031 *cond_block_tf_writemask
|=
1032 get_block_tessfactor_writemask(info
, parse
, TGSI_OPCODE_ENDLOOP
);
1035 case TGSI_OPCODE_BARRIER
:
1036 unreachable("nested BARRIER is illegal");
1040 /* Process an instruction in the current block. */
1041 unsigned writemask
= get_inst_tessfactor_writemask(info
, inst
);
1045 then_tessfactor_writemask
|= writemask
;
1047 else_tessfactor_writemask
|= writemask
;
1049 } while (inst
->Instruction
.Opcode
!= TGSI_OPCODE_ENDIF
);
1051 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
1052 /* If both statements write the same tess factor channels,
1053 * we can say that the upper block writes them too. */
1054 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
1055 else_tessfactor_writemask
;
1056 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
1057 else_tessfactor_writemask
;
1062 tgsi_scan_tess_ctrl(const struct tgsi_token
*tokens
,
1063 const struct tgsi_shader_info
*info
,
1064 struct tgsi_tessctrl_info
*out
)
1066 memset(out
, 0, sizeof(*out
));
1068 if (info
->processor
!= PIPE_SHADER_TESS_CTRL
)
1071 struct tgsi_parse_context parse
;
1072 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
1073 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
1077 /* The pass works as follows:
1078 * If all codepaths write tess factors, we can say that all invocations
1079 * define tess factors.
1081 * Each tess factor channel is tracked separately.
1083 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
1084 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
1086 /* Initial value = true. Here the pass will accumulate results from multiple
1087 * segments surrounded by barriers. If tess factors aren't written at all,
1088 * it's a shader bug and we don't care if this will be true.
1090 out
->tessfactors_are_def_in_all_invocs
= true;
1092 while (!tgsi_parse_end_of_tokens(&parse
)) {
1093 tgsi_parse_token(&parse
);
1095 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
1098 struct tgsi_full_instruction
*inst
= &parse
.FullToken
.FullInstruction
;
1099 check_no_subroutines(inst
);
1101 /* Process nested blocks. */
1102 switch (inst
->Instruction
.Opcode
) {
1103 case TGSI_OPCODE_IF
:
1104 case TGSI_OPCODE_UIF
:
1105 get_if_block_tessfactor_writemask(info
, &parse
,
1106 &main_block_tf_writemask
,
1107 &cond_block_tf_writemask
);
1110 case TGSI_OPCODE_BGNLOOP
:
1111 cond_block_tf_writemask
|=
1112 get_block_tessfactor_writemask(info
, &parse
, TGSI_OPCODE_ENDIF
);
1115 case TGSI_OPCODE_BARRIER
:
1116 /* The following case must be prevented:
1117 * gl_TessLevelInner = ...;
1119 * if (gl_InvocationID == 1)
1120 * gl_TessLevelInner = ...;
1122 * If you consider disjoint code segments separated by barriers, each
1123 * such segment that writes tess factor channels should write the same
1124 * channels in all codepaths within that segment.
1126 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
1127 /* Accumulate the result: */
1128 out
->tessfactors_are_def_in_all_invocs
&=
1129 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
1131 /* Analyze the next code segment from scratch. */
1132 main_block_tf_writemask
= 0;
1133 cond_block_tf_writemask
= 0;
1138 main_block_tf_writemask
|= get_inst_tessfactor_writemask(info
, inst
);
1141 /* Accumulate the result for the last code segment separated by a barrier. */
1142 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
1143 out
->tessfactors_are_def_in_all_invocs
&=
1144 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
1147 tgsi_parse_free(&parse
);