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
||
54 file
== TGSI_FILE_HW_ATOMIC
;
59 is_mem_query_inst(enum tgsi_opcode opcode
)
61 return opcode
== TGSI_OPCODE_RESQ
||
62 opcode
== TGSI_OPCODE_TXQ
||
63 opcode
== TGSI_OPCODE_TXQS
||
64 opcode
== TGSI_OPCODE_LODQ
;
68 * Is the opcode a "true" texture instruction which samples from a
72 is_texture_inst(enum tgsi_opcode 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(enum tgsi_opcode 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_TXQS
;
98 return opcode
== TGSI_OPCODE_DDX
|| opcode
== TGSI_OPCODE_DDX_FINE
||
99 opcode
== TGSI_OPCODE_DDY
|| opcode
== TGSI_OPCODE_DDY_FINE
||
100 opcode
== TGSI_OPCODE_SAMPLE
||
101 opcode
== TGSI_OPCODE_SAMPLE_B
||
102 opcode
== TGSI_OPCODE_SAMPLE_C
;
107 scan_src_operand(struct tgsi_shader_info
*info
,
108 const struct tgsi_full_instruction
*fullinst
,
109 const struct tgsi_full_src_register
*src
,
111 unsigned usage_mask_after_swizzle
,
112 bool is_interp_instruction
,
115 int ind
= src
->Register
.Index
;
117 if (info
->processor
== PIPE_SHADER_COMPUTE
&&
118 src
->Register
.File
== TGSI_FILE_SYSTEM_VALUE
) {
121 name
= info
->system_value_semantic_name
[src
->Register
.Index
];
124 case TGSI_SEMANTIC_THREAD_ID
:
125 case TGSI_SEMANTIC_BLOCK_ID
:
126 mask
= usage_mask_after_swizzle
& TGSI_WRITEMASK_XYZ
;
128 unsigned i
= u_bit_scan(&mask
);
130 if (name
== TGSI_SEMANTIC_THREAD_ID
)
131 info
->uses_thread_id
[i
] = true;
133 info
->uses_block_id
[i
] = true;
136 case TGSI_SEMANTIC_BLOCK_SIZE
:
137 /* The block size is translated to IMM with a fixed block size. */
138 if (info
->properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] == 0)
139 info
->uses_block_size
= true;
141 case TGSI_SEMANTIC_GRID_SIZE
:
142 info
->uses_grid_size
= true;
147 /* Mark which inputs are effectively used */
148 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
149 if (src
->Register
.Indirect
) {
150 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
151 info
->input_usage_mask
[ind
] |= usage_mask_after_swizzle
;
155 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
156 info
->input_usage_mask
[ind
] |= usage_mask_after_swizzle
;
159 if (info
->processor
== PIPE_SHADER_FRAGMENT
) {
160 unsigned name
, index
, input
;
162 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
163 input
= info
->input_array_first
[src
->Indirect
.ArrayID
];
165 input
= src
->Register
.Index
;
167 name
= info
->input_semantic_name
[input
];
168 index
= info
->input_semantic_index
[input
];
170 if (name
== TGSI_SEMANTIC_POSITION
&&
171 usage_mask_after_swizzle
& TGSI_WRITEMASK_Z
)
172 info
->reads_z
= true;
174 if (name
== TGSI_SEMANTIC_COLOR
)
175 info
->colors_read
|= usage_mask_after_swizzle
<< (index
* 4);
177 /* Process only interpolated varyings. Don't include POSITION.
178 * Don't include integer varyings, because they are not
179 * interpolated. Don't process inputs interpolated by INTERP
180 * opcodes. Those are tracked separately.
182 if ((!is_interp_instruction
|| src_index
!= 0) &&
183 (name
== TGSI_SEMANTIC_GENERIC
||
184 name
== TGSI_SEMANTIC_TEXCOORD
||
185 name
== TGSI_SEMANTIC_COLOR
||
186 name
== TGSI_SEMANTIC_BCOLOR
||
187 name
== TGSI_SEMANTIC_FOG
||
188 name
== TGSI_SEMANTIC_CLIPDIST
)) {
189 switch (info
->input_interpolate
[input
]) {
190 case TGSI_INTERPOLATE_COLOR
:
191 case TGSI_INTERPOLATE_PERSPECTIVE
:
192 switch (info
->input_interpolate_loc
[input
]) {
193 case TGSI_INTERPOLATE_LOC_CENTER
:
194 info
->uses_persp_center
= TRUE
;
196 case TGSI_INTERPOLATE_LOC_CENTROID
:
197 info
->uses_persp_centroid
= TRUE
;
199 case TGSI_INTERPOLATE_LOC_SAMPLE
:
200 info
->uses_persp_sample
= TRUE
;
204 case TGSI_INTERPOLATE_LINEAR
:
205 switch (info
->input_interpolate_loc
[input
]) {
206 case TGSI_INTERPOLATE_LOC_CENTER
:
207 info
->uses_linear_center
= TRUE
;
209 case TGSI_INTERPOLATE_LOC_CENTROID
:
210 info
->uses_linear_centroid
= TRUE
;
212 case TGSI_INTERPOLATE_LOC_SAMPLE
:
213 info
->uses_linear_sample
= TRUE
;
217 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
223 if (info
->processor
== PIPE_SHADER_TESS_CTRL
&&
224 src
->Register
.File
== TGSI_FILE_OUTPUT
) {
227 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
228 input
= info
->output_array_first
[src
->Indirect
.ArrayID
];
230 input
= src
->Register
.Index
;
232 switch (info
->output_semantic_name
[input
]) {
233 case TGSI_SEMANTIC_PATCH
:
234 info
->reads_perpatch_outputs
= true;
236 case TGSI_SEMANTIC_TESSINNER
:
237 case TGSI_SEMANTIC_TESSOUTER
:
238 info
->reads_tessfactor_outputs
= true;
241 info
->reads_pervertex_outputs
= true;
245 /* check for indirect register reads */
246 if (src
->Register
.Indirect
) {
247 info
->indirect_files
|= (1 << src
->Register
.File
);
248 info
->indirect_files_read
|= (1 << src
->Register
.File
);
250 /* record indirect constant buffer indexing */
251 if (src
->Register
.File
== TGSI_FILE_CONSTANT
) {
252 if (src
->Register
.Dimension
) {
253 if (src
->Dimension
.Indirect
)
254 info
->const_buffers_indirect
= info
->const_buffers_declared
;
256 info
->const_buffers_indirect
|= 1u << src
->Dimension
.Index
;
258 info
->const_buffers_indirect
|= 1;
263 if (src
->Register
.Dimension
&& src
->Dimension
.Indirect
)
264 info
->dim_indirect_files
|= 1u << src
->Register
.File
;
266 /* Texture samplers */
267 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
268 const unsigned index
= src
->Register
.Index
;
270 assert(fullinst
->Instruction
.Texture
);
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
);
292 if (is_memory_file(src
->Register
.File
) &&
293 !is_mem_query_inst(fullinst
->Instruction
.Opcode
)) {
296 if (tgsi_get_opcode_info(fullinst
->Instruction
.Opcode
)->is_store
) {
297 info
->writes_memory
= TRUE
;
299 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
300 if (src
->Register
.Indirect
)
301 info
->images_atomic
= info
->images_declared
;
303 info
->images_atomic
|= 1 << src
->Register
.Index
;
304 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
305 if (src
->Register
.Indirect
)
306 info
->shader_buffers_atomic
= info
->shader_buffers_declared
;
308 info
->shader_buffers_atomic
|= 1 << src
->Register
.Index
;
311 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
312 if (src
->Register
.Indirect
)
313 info
->images_load
= info
->images_declared
;
315 info
->images_load
|= 1 << src
->Register
.Index
;
316 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
317 if (src
->Register
.Indirect
)
318 info
->shader_buffers_load
= info
->shader_buffers_declared
;
320 info
->shader_buffers_load
|= 1 << src
->Register
.Index
;
328 scan_instruction(struct tgsi_shader_info
*info
,
329 const struct tgsi_full_instruction
*fullinst
,
330 unsigned *current_depth
)
333 bool is_mem_inst
= false;
334 bool is_interp_instruction
= false;
335 unsigned sampler_src
;
337 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
338 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
340 switch (fullinst
->Instruction
.Opcode
) {
342 case TGSI_OPCODE_UIF
:
343 case TGSI_OPCODE_BGNLOOP
:
345 info
->max_depth
= MAX2(info
->max_depth
, *current_depth
);
347 case TGSI_OPCODE_ENDIF
:
348 case TGSI_OPCODE_ENDLOOP
:
351 case TGSI_OPCODE_TEX
:
352 case TGSI_OPCODE_TEX_LZ
:
353 case TGSI_OPCODE_TXB
:
354 case TGSI_OPCODE_TXD
:
355 case TGSI_OPCODE_TXL
:
356 case TGSI_OPCODE_TXP
:
357 case TGSI_OPCODE_TXQ
:
358 case TGSI_OPCODE_TXQS
:
359 case TGSI_OPCODE_TXF
:
360 case TGSI_OPCODE_TXF_LZ
:
361 case TGSI_OPCODE_TEX2
:
362 case TGSI_OPCODE_TXB2
:
363 case TGSI_OPCODE_TXL2
:
364 case TGSI_OPCODE_TG4
:
365 case TGSI_OPCODE_LODQ
:
366 sampler_src
= fullinst
->Instruction
.NumSrcRegs
- 1;
367 if (fullinst
->Src
[sampler_src
].Register
.File
!= TGSI_FILE_SAMPLER
)
368 info
->uses_bindless_samplers
= true;
370 case TGSI_OPCODE_RESQ
:
371 case TGSI_OPCODE_LOAD
:
372 case TGSI_OPCODE_ATOMUADD
:
373 case TGSI_OPCODE_ATOMXCHG
:
374 case TGSI_OPCODE_ATOMCAS
:
375 case TGSI_OPCODE_ATOMAND
:
376 case TGSI_OPCODE_ATOMOR
:
377 case TGSI_OPCODE_ATOMXOR
:
378 case TGSI_OPCODE_ATOMUMIN
:
379 case TGSI_OPCODE_ATOMUMAX
:
380 case TGSI_OPCODE_ATOMIMIN
:
381 case TGSI_OPCODE_ATOMIMAX
:
382 if (tgsi_is_bindless_image_file(fullinst
->Src
[0].Register
.File
))
383 info
->uses_bindless_images
= true;
385 case TGSI_OPCODE_STORE
:
386 if (tgsi_is_bindless_image_file(fullinst
->Dst
[0].Register
.File
))
387 info
->uses_bindless_images
= true;
393 if (fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_CENTROID
||
394 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
395 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
396 const struct tgsi_full_src_register
*src0
= &fullinst
->Src
[0];
399 is_interp_instruction
= true;
401 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
402 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
404 input
= src0
->Register
.Index
;
406 /* For the INTERP opcodes, the interpolation is always
407 * PERSPECTIVE unless LINEAR is specified.
409 switch (info
->input_interpolate
[input
]) {
410 case TGSI_INTERPOLATE_COLOR
:
411 case TGSI_INTERPOLATE_CONSTANT
:
412 case TGSI_INTERPOLATE_PERSPECTIVE
:
413 switch (fullinst
->Instruction
.Opcode
) {
414 case TGSI_OPCODE_INTERP_CENTROID
:
415 info
->uses_persp_opcode_interp_centroid
= TRUE
;
417 case TGSI_OPCODE_INTERP_OFFSET
:
418 info
->uses_persp_opcode_interp_offset
= TRUE
;
420 case TGSI_OPCODE_INTERP_SAMPLE
:
421 info
->uses_persp_opcode_interp_sample
= TRUE
;
426 case TGSI_INTERPOLATE_LINEAR
:
427 switch (fullinst
->Instruction
.Opcode
) {
428 case TGSI_OPCODE_INTERP_CENTROID
:
429 info
->uses_linear_opcode_interp_centroid
= TRUE
;
431 case TGSI_OPCODE_INTERP_OFFSET
:
432 info
->uses_linear_opcode_interp_offset
= TRUE
;
434 case TGSI_OPCODE_INTERP_SAMPLE
:
435 info
->uses_linear_opcode_interp_sample
= TRUE
;
442 if ((fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
443 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
) ||
444 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_DFMA
||
445 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_DDIV
||
446 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_D2U64
||
447 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_D2I64
||
448 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_U642D
||
449 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_I642D
)
450 info
->uses_doubles
= TRUE
;
452 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
453 scan_src_operand(info
, fullinst
, &fullinst
->Src
[i
], i
,
454 tgsi_util_get_inst_usage_mask(fullinst
, i
),
455 is_interp_instruction
, &is_mem_inst
);
457 if (fullinst
->Src
[i
].Register
.Indirect
) {
458 struct tgsi_full_src_register src
= {{0}};
460 src
.Register
.File
= fullinst
->Src
[i
].Indirect
.File
;
461 src
.Register
.Index
= fullinst
->Src
[i
].Indirect
.Index
;
463 scan_src_operand(info
, fullinst
, &src
, -1,
464 1 << fullinst
->Src
[i
].Indirect
.Swizzle
,
468 if (fullinst
->Src
[i
].Register
.Dimension
&&
469 fullinst
->Src
[i
].Dimension
.Indirect
) {
470 struct tgsi_full_src_register src
= {{0}};
472 src
.Register
.File
= fullinst
->Src
[i
].DimIndirect
.File
;
473 src
.Register
.Index
= fullinst
->Src
[i
].DimIndirect
.Index
;
475 scan_src_operand(info
, fullinst
, &src
, -1,
476 1 << fullinst
->Src
[i
].DimIndirect
.Swizzle
,
481 if (fullinst
->Instruction
.Texture
) {
482 for (i
= 0; i
< fullinst
->Texture
.NumOffsets
; i
++) {
483 struct tgsi_full_src_register src
= {{0}};
485 src
.Register
.File
= fullinst
->TexOffsets
[i
].File
;
486 src
.Register
.Index
= fullinst
->TexOffsets
[i
].Index
;
488 /* The usage mask is suboptimal but should be safe. */
489 scan_src_operand(info
, fullinst
, &src
, -1,
490 (1 << fullinst
->TexOffsets
[i
].SwizzleX
) |
491 (1 << fullinst
->TexOffsets
[i
].SwizzleY
) |
492 (1 << fullinst
->TexOffsets
[i
].SwizzleZ
),
493 false, &is_mem_inst
);
497 /* check for indirect register writes */
498 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
499 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
501 if (dst
->Register
.Indirect
) {
502 struct tgsi_full_src_register src
= {{0}};
504 src
.Register
.File
= dst
->Indirect
.File
;
505 src
.Register
.Index
= dst
->Indirect
.Index
;
507 scan_src_operand(info
, fullinst
, &src
, -1,
508 1 << dst
->Indirect
.Swizzle
, false, NULL
);
510 info
->indirect_files
|= (1 << dst
->Register
.File
);
511 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
514 if (dst
->Register
.Dimension
&& dst
->Dimension
.Indirect
) {
515 struct tgsi_full_src_register src
= {{0}};
517 src
.Register
.File
= dst
->DimIndirect
.File
;
518 src
.Register
.Index
= dst
->DimIndirect
.Index
;
520 scan_src_operand(info
, fullinst
, &src
, -1,
521 1 << dst
->DimIndirect
.Swizzle
, false, NULL
);
523 info
->dim_indirect_files
|= 1u << dst
->Register
.File
;
526 if (is_memory_file(dst
->Register
.File
)) {
527 assert(fullinst
->Instruction
.Opcode
== TGSI_OPCODE_STORE
);
530 info
->writes_memory
= TRUE
;
532 if (dst
->Register
.File
== TGSI_FILE_IMAGE
) {
533 if (dst
->Register
.Indirect
)
534 info
->images_store
= info
->images_declared
;
536 info
->images_store
|= 1 << dst
->Register
.Index
;
537 } else if (dst
->Register
.File
== TGSI_FILE_BUFFER
) {
538 if (dst
->Register
.Indirect
)
539 info
->shader_buffers_store
= info
->shader_buffers_declared
;
541 info
->shader_buffers_store
|= 1 << dst
->Register
.Index
;
547 info
->num_memory_instructions
++;
549 if (computes_derivative(fullinst
->Instruction
.Opcode
))
550 info
->uses_derivatives
= true;
552 info
->num_instructions
++;
557 scan_declaration(struct tgsi_shader_info
*info
,
558 const struct tgsi_full_declaration
*fulldecl
)
560 const uint file
= fulldecl
->Declaration
.File
;
561 const unsigned procType
= info
->processor
;
564 if (fulldecl
->Declaration
.Array
) {
565 unsigned array_id
= fulldecl
->Array
.ArrayID
;
568 case TGSI_FILE_INPUT
:
569 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
570 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
571 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
573 case TGSI_FILE_OUTPUT
:
574 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
575 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
576 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
579 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
582 for (reg
= fulldecl
->Range
.First
; reg
<= fulldecl
->Range
.Last
; reg
++) {
583 unsigned semName
= fulldecl
->Semantic
.Name
;
584 unsigned semIndex
= fulldecl
->Semantic
.Index
+
585 (reg
- fulldecl
->Range
.First
);
587 unsigned index
, target
, type
;
590 * only first 32 regs will appear in this bitfield, if larger
591 * bits will wrap around.
593 info
->file_mask
[file
] |= (1u << (reg
& 31));
594 info
->file_count
[file
]++;
595 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
598 case TGSI_FILE_CONSTANT
:
601 if (fulldecl
->Declaration
.Dimension
)
602 buffer
= fulldecl
->Dim
.Index2D
;
604 info
->const_file_max
[buffer
] =
605 MAX2(info
->const_file_max
[buffer
], (int)reg
);
606 info
->const_buffers_declared
|= 1u << buffer
;
609 case TGSI_FILE_IMAGE
:
610 info
->images_declared
|= 1u << reg
;
611 if (fulldecl
->Image
.Resource
== TGSI_TEXTURE_BUFFER
)
612 info
->images_buffers
|= 1 << reg
;
615 case TGSI_FILE_BUFFER
:
616 info
->shader_buffers_declared
|= 1u << reg
;
619 case TGSI_FILE_INPUT
:
620 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
621 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
622 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
623 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
624 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
626 /* Vertex shaders can have inputs with holes between them. */
627 info
->num_inputs
= MAX2(info
->num_inputs
, reg
+ 1);
630 case TGSI_SEMANTIC_PRIMID
:
631 info
->uses_primid
= true;
633 case TGSI_SEMANTIC_POSITION
:
634 info
->reads_position
= true;
636 case TGSI_SEMANTIC_FACE
:
637 info
->uses_frontface
= true;
642 case TGSI_FILE_SYSTEM_VALUE
:
643 index
= fulldecl
->Range
.First
;
645 info
->system_value_semantic_name
[index
] = semName
;
646 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
649 case TGSI_SEMANTIC_INSTANCEID
:
650 info
->uses_instanceid
= TRUE
;
652 case TGSI_SEMANTIC_VERTEXID
:
653 info
->uses_vertexid
= TRUE
;
655 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
656 info
->uses_vertexid_nobase
= TRUE
;
658 case TGSI_SEMANTIC_BASEVERTEX
:
659 info
->uses_basevertex
= TRUE
;
661 case TGSI_SEMANTIC_PRIMID
:
662 info
->uses_primid
= TRUE
;
664 case TGSI_SEMANTIC_INVOCATIONID
:
665 info
->uses_invocationid
= TRUE
;
667 case TGSI_SEMANTIC_POSITION
:
668 info
->reads_position
= TRUE
;
670 case TGSI_SEMANTIC_FACE
:
671 info
->uses_frontface
= TRUE
;
673 case TGSI_SEMANTIC_SAMPLEMASK
:
674 info
->reads_samplemask
= TRUE
;
676 case TGSI_SEMANTIC_TESSINNER
:
677 case TGSI_SEMANTIC_TESSOUTER
:
678 info
->reads_tess_factors
= true;
683 case TGSI_FILE_OUTPUT
:
684 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
685 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
686 info
->output_usagemask
[reg
] |= fulldecl
->Declaration
.UsageMask
;
687 info
->num_outputs
= MAX2(info
->num_outputs
, reg
+ 1);
689 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_X
) {
690 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamX
;
691 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamX
]++;
693 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Y
) {
694 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamY
<< 2;
695 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamY
]++;
697 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Z
) {
698 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamZ
<< 4;
699 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamZ
]++;
701 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_W
) {
702 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamW
<< 6;
703 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamW
]++;
707 case TGSI_SEMANTIC_PRIMID
:
708 info
->writes_primid
= true;
710 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
711 info
->writes_viewport_index
= true;
713 case TGSI_SEMANTIC_LAYER
:
714 info
->writes_layer
= true;
716 case TGSI_SEMANTIC_PSIZE
:
717 info
->writes_psize
= true;
719 case TGSI_SEMANTIC_CLIPVERTEX
:
720 info
->writes_clipvertex
= true;
722 case TGSI_SEMANTIC_COLOR
:
723 info
->colors_written
|= 1 << semIndex
;
725 case TGSI_SEMANTIC_STENCIL
:
726 info
->writes_stencil
= true;
728 case TGSI_SEMANTIC_SAMPLEMASK
:
729 info
->writes_samplemask
= true;
731 case TGSI_SEMANTIC_EDGEFLAG
:
732 info
->writes_edgeflag
= true;
734 case TGSI_SEMANTIC_POSITION
:
735 if (procType
== PIPE_SHADER_FRAGMENT
)
736 info
->writes_z
= true;
738 info
->writes_position
= true;
743 case TGSI_FILE_SAMPLER
:
744 STATIC_ASSERT(sizeof(info
->samplers_declared
) * 8 >= PIPE_MAX_SAMPLERS
);
745 info
->samplers_declared
|= 1u << reg
;
748 case TGSI_FILE_SAMPLER_VIEW
:
749 target
= fulldecl
->SamplerView
.Resource
;
750 type
= fulldecl
->SamplerView
.ReturnTypeX
;
752 assert(target
< TGSI_TEXTURE_UNKNOWN
);
753 if (info
->sampler_targets
[reg
] == TGSI_TEXTURE_UNKNOWN
) {
754 /* Save sampler target for this sampler index */
755 info
->sampler_targets
[reg
] = target
;
756 info
->sampler_type
[reg
] = type
;
758 /* if previously declared, make sure targets agree */
759 assert(info
->sampler_targets
[reg
] == target
);
760 assert(info
->sampler_type
[reg
] == type
);
769 scan_immediate(struct tgsi_shader_info
*info
)
771 uint reg
= info
->immediate_count
++;
772 uint file
= TGSI_FILE_IMMEDIATE
;
774 info
->file_mask
[file
] |= (1 << reg
);
775 info
->file_count
[file
]++;
776 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
781 scan_property(struct tgsi_shader_info
*info
,
782 const struct tgsi_full_property
*fullprop
)
784 unsigned name
= fullprop
->Property
.PropertyName
;
785 unsigned value
= fullprop
->u
[0].Data
;
787 assert(name
< ARRAY_SIZE(info
->properties
));
788 info
->properties
[name
] = value
;
791 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
792 info
->num_written_clipdistance
= value
;
793 info
->clipdist_writemask
|= (1 << value
) - 1;
795 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
796 info
->num_written_culldistance
= value
;
797 info
->culldist_writemask
|= (1 << value
) - 1;
804 * Scan the given TGSI shader to collect information such as number of
805 * registers used, special instructions used, etc.
806 * \return info the result of the scan
809 tgsi_scan_shader(const struct tgsi_token
*tokens
,
810 struct tgsi_shader_info
*info
)
813 struct tgsi_parse_context parse
;
814 unsigned current_depth
= 0;
816 memset(info
, 0, sizeof(*info
));
817 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
818 info
->file_max
[i
] = -1;
819 for (i
= 0; i
< ARRAY_SIZE(info
->const_file_max
); i
++)
820 info
->const_file_max
[i
] = -1;
821 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
822 for (i
= 0; i
< ARRAY_SIZE(info
->sampler_targets
); i
++)
823 info
->sampler_targets
[i
] = TGSI_TEXTURE_UNKNOWN
;
826 ** Setup to begin parsing input shader
828 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
829 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
832 procType
= parse
.FullHeader
.Processor
.Processor
;
833 assert(procType
== PIPE_SHADER_FRAGMENT
||
834 procType
== PIPE_SHADER_VERTEX
||
835 procType
== PIPE_SHADER_GEOMETRY
||
836 procType
== PIPE_SHADER_TESS_CTRL
||
837 procType
== PIPE_SHADER_TESS_EVAL
||
838 procType
== PIPE_SHADER_COMPUTE
);
839 info
->processor
= procType
;
840 info
->num_tokens
= tgsi_num_tokens(parse
.Tokens
);
843 ** Loop over incoming program tokens/instructions
845 while (!tgsi_parse_end_of_tokens(&parse
)) {
846 tgsi_parse_token( &parse
);
848 switch( parse
.FullToken
.Token
.Type
) {
849 case TGSI_TOKEN_TYPE_INSTRUCTION
:
850 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
853 case TGSI_TOKEN_TYPE_DECLARATION
:
854 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
856 case TGSI_TOKEN_TYPE_IMMEDIATE
:
857 scan_immediate(info
);
859 case TGSI_TOKEN_TYPE_PROPERTY
:
860 scan_property(info
, &parse
.FullToken
.FullProperty
);
863 assert(!"Unexpected TGSI token type");
867 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
868 info
->opcode_count
[TGSI_OPCODE_KILL
]);
870 /* The dimensions of the IN decleration in geometry shader have
871 * to be deduced from the type of the input primitive.
873 if (procType
== PIPE_SHADER_GEOMETRY
) {
874 unsigned input_primitive
=
875 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
876 int num_verts
= u_vertices_per_prim(input_primitive
);
878 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
879 info
->file_max
[TGSI_FILE_INPUT
] =
880 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
881 for (j
= 0; j
< num_verts
; ++j
) {
882 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
886 tgsi_parse_free(&parse
);
890 * Collect information about the arrays of a given register file.
892 * @param tokens TGSI shader
893 * @param file the register file to scan through
894 * @param max_array_id number of entries in @p arrays; should be equal to the
895 * highest array id, i.e. tgsi_shader_info::array_max[file].
896 * @param arrays info for array of each ID will be written to arrays[ID - 1].
899 tgsi_scan_arrays(const struct tgsi_token
*tokens
,
901 unsigned max_array_id
,
902 struct tgsi_array_info
*arrays
)
904 struct tgsi_parse_context parse
;
906 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
907 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
911 memset(arrays
, 0, sizeof(arrays
[0]) * max_array_id
);
913 while (!tgsi_parse_end_of_tokens(&parse
)) {
914 struct tgsi_full_instruction
*inst
;
916 tgsi_parse_token(&parse
);
918 if (parse
.FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
919 struct tgsi_full_declaration
*decl
= &parse
.FullToken
.FullDeclaration
;
921 if (decl
->Declaration
.Array
&& decl
->Declaration
.File
== file
&&
922 decl
->Array
.ArrayID
> 0 && decl
->Array
.ArrayID
<= max_array_id
) {
923 struct tgsi_array_info
*array
= &arrays
[decl
->Array
.ArrayID
- 1];
924 assert(!array
->declared
);
925 array
->declared
= true;
926 array
->range
= decl
->Range
;
930 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
933 inst
= &parse
.FullToken
.FullInstruction
;
934 for (unsigned i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
935 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
936 if (dst
->Register
.File
!= file
)
939 if (dst
->Register
.Indirect
) {
940 if (dst
->Indirect
.ArrayID
> 0 &&
941 dst
->Indirect
.ArrayID
<= max_array_id
) {
942 arrays
[dst
->Indirect
.ArrayID
- 1].writemask
|= dst
->Register
.WriteMask
;
944 /* Indirect writes without an ArrayID can write anywhere. */
945 for (unsigned j
= 0; j
< max_array_id
; ++j
)
946 arrays
[j
].writemask
|= dst
->Register
.WriteMask
;
949 /* Check whether the write falls into any of the arrays anyway. */
950 for (unsigned j
= 0; j
< max_array_id
; ++j
) {
951 struct tgsi_array_info
*array
= &arrays
[j
];
952 if (array
->declared
&&
953 dst
->Register
.Index
>= array
->range
.First
&&
954 dst
->Register
.Index
<= array
->range
.Last
)
955 array
->writemask
|= dst
->Register
.WriteMask
;
961 tgsi_parse_free(&parse
);
967 check_no_subroutines(const struct tgsi_full_instruction
*inst
)
969 switch (inst
->Instruction
.Opcode
) {
970 case TGSI_OPCODE_BGNSUB
:
971 case TGSI_OPCODE_ENDSUB
:
972 case TGSI_OPCODE_CAL
:
973 unreachable("subroutines unhandled");
978 get_inst_tessfactor_writemask(const struct tgsi_shader_info
*info
,
979 const struct tgsi_full_instruction
*inst
)
981 unsigned writemask
= 0;
983 for (unsigned i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
984 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
986 if (dst
->Register
.File
== TGSI_FILE_OUTPUT
&&
987 !dst
->Register
.Indirect
) {
988 unsigned name
= info
->output_semantic_name
[dst
->Register
.Index
];
990 if (name
== TGSI_SEMANTIC_TESSINNER
)
991 writemask
|= dst
->Register
.WriteMask
;
992 else if (name
== TGSI_SEMANTIC_TESSOUTER
)
993 writemask
|= dst
->Register
.WriteMask
<< 4;
1000 get_block_tessfactor_writemask(const struct tgsi_shader_info
*info
,
1001 struct tgsi_parse_context
*parse
,
1002 unsigned end_opcode
)
1004 struct tgsi_full_instruction
*inst
;
1005 unsigned writemask
= 0;
1008 tgsi_parse_token(parse
);
1009 assert(parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
1010 inst
= &parse
->FullToken
.FullInstruction
;
1011 check_no_subroutines(inst
);
1013 /* Recursively process nested blocks. */
1014 switch (inst
->Instruction
.Opcode
) {
1015 case TGSI_OPCODE_IF
:
1016 case TGSI_OPCODE_UIF
:
1018 get_block_tessfactor_writemask(info
, parse
, TGSI_OPCODE_ENDIF
);
1021 case TGSI_OPCODE_BGNLOOP
:
1023 get_block_tessfactor_writemask(info
, parse
, TGSI_OPCODE_ENDLOOP
);
1026 case TGSI_OPCODE_BARRIER
:
1027 unreachable("nested BARRIER is illegal");
1031 writemask
|= get_inst_tessfactor_writemask(info
, inst
);
1032 } while (inst
->Instruction
.Opcode
!= end_opcode
);
1038 get_if_block_tessfactor_writemask(const struct tgsi_shader_info
*info
,
1039 struct tgsi_parse_context
*parse
,
1040 unsigned *upper_block_tf_writemask
,
1041 unsigned *cond_block_tf_writemask
)
1043 struct tgsi_full_instruction
*inst
;
1044 unsigned then_tessfactor_writemask
= 0;
1045 unsigned else_tessfactor_writemask
= 0;
1046 bool is_then
= true;
1049 tgsi_parse_token(parse
);
1050 assert(parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
1051 inst
= &parse
->FullToken
.FullInstruction
;
1052 check_no_subroutines(inst
);
1054 switch (inst
->Instruction
.Opcode
) {
1055 case TGSI_OPCODE_ELSE
:
1059 /* Recursively process nested blocks. */
1060 case TGSI_OPCODE_IF
:
1061 case TGSI_OPCODE_UIF
:
1062 get_if_block_tessfactor_writemask(info
, parse
,
1063 is_then
? &then_tessfactor_writemask
:
1064 &else_tessfactor_writemask
,
1065 cond_block_tf_writemask
);
1068 case TGSI_OPCODE_BGNLOOP
:
1069 *cond_block_tf_writemask
|=
1070 get_block_tessfactor_writemask(info
, parse
, TGSI_OPCODE_ENDLOOP
);
1073 case TGSI_OPCODE_BARRIER
:
1074 unreachable("nested BARRIER is illegal");
1078 /* Process an instruction in the current block. */
1079 unsigned writemask
= get_inst_tessfactor_writemask(info
, inst
);
1083 then_tessfactor_writemask
|= writemask
;
1085 else_tessfactor_writemask
|= writemask
;
1087 } while (inst
->Instruction
.Opcode
!= TGSI_OPCODE_ENDIF
);
1089 if (then_tessfactor_writemask
|| else_tessfactor_writemask
) {
1090 /* If both statements write the same tess factor channels,
1091 * we can say that the upper block writes them too. */
1092 *upper_block_tf_writemask
|= then_tessfactor_writemask
&
1093 else_tessfactor_writemask
;
1094 *cond_block_tf_writemask
|= then_tessfactor_writemask
|
1095 else_tessfactor_writemask
;
1100 tgsi_scan_tess_ctrl(const struct tgsi_token
*tokens
,
1101 const struct tgsi_shader_info
*info
,
1102 struct tgsi_tessctrl_info
*out
)
1104 memset(out
, 0, sizeof(*out
));
1106 if (info
->processor
!= PIPE_SHADER_TESS_CTRL
)
1109 struct tgsi_parse_context parse
;
1110 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
1111 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
1115 /* The pass works as follows:
1116 * If all codepaths write tess factors, we can say that all invocations
1117 * define tess factors.
1119 * Each tess factor channel is tracked separately.
1121 unsigned main_block_tf_writemask
= 0; /* if main block writes tess factors */
1122 unsigned cond_block_tf_writemask
= 0; /* if cond block writes tess factors */
1124 /* Initial value = true. Here the pass will accumulate results from multiple
1125 * segments surrounded by barriers. If tess factors aren't written at all,
1126 * it's a shader bug and we don't care if this will be true.
1128 out
->tessfactors_are_def_in_all_invocs
= true;
1130 while (!tgsi_parse_end_of_tokens(&parse
)) {
1131 tgsi_parse_token(&parse
);
1133 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
1136 struct tgsi_full_instruction
*inst
= &parse
.FullToken
.FullInstruction
;
1137 check_no_subroutines(inst
);
1139 /* Process nested blocks. */
1140 switch (inst
->Instruction
.Opcode
) {
1141 case TGSI_OPCODE_IF
:
1142 case TGSI_OPCODE_UIF
:
1143 get_if_block_tessfactor_writemask(info
, &parse
,
1144 &main_block_tf_writemask
,
1145 &cond_block_tf_writemask
);
1148 case TGSI_OPCODE_BGNLOOP
:
1149 cond_block_tf_writemask
|=
1150 get_block_tessfactor_writemask(info
, &parse
, TGSI_OPCODE_ENDIF
);
1153 case TGSI_OPCODE_BARRIER
:
1154 /* The following case must be prevented:
1155 * gl_TessLevelInner = ...;
1157 * if (gl_InvocationID == 1)
1158 * gl_TessLevelInner = ...;
1160 * If you consider disjoint code segments separated by barriers, each
1161 * such segment that writes tess factor channels should write the same
1162 * channels in all codepaths within that segment.
1164 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
1165 /* Accumulate the result: */
1166 out
->tessfactors_are_def_in_all_invocs
&=
1167 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
1169 /* Analyze the next code segment from scratch. */
1170 main_block_tf_writemask
= 0;
1171 cond_block_tf_writemask
= 0;
1176 main_block_tf_writemask
|= get_inst_tessfactor_writemask(info
, inst
);
1179 /* Accumulate the result for the last code segment separated by a barrier. */
1180 if (main_block_tf_writemask
|| cond_block_tf_writemask
) {
1181 out
->tessfactors_are_def_in_all_invocs
&=
1182 !(cond_block_tf_writemask
& ~main_block_tf_writemask
);
1185 tgsi_parse_free(&parse
);