1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
5 * Copyright 2008 VMware, Inc. All rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
30 * TGSI program scan utility.
31 * Used to determine which registers and instructions are used by a shader.
37 #include "util/u_debug.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40 #include "util/u_prim.h"
41 #include "tgsi/tgsi_info.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_util.h"
44 #include "tgsi/tgsi_scan.h"
48 is_memory_file(unsigned file
)
50 return file
== TGSI_FILE_SAMPLER
||
51 file
== TGSI_FILE_SAMPLER_VIEW
||
52 file
== TGSI_FILE_IMAGE
||
53 file
== TGSI_FILE_BUFFER
;
58 is_mem_query_inst(unsigned opcode
)
60 return opcode
== TGSI_OPCODE_RESQ
||
61 opcode
== TGSI_OPCODE_TXQ
||
62 opcode
== TGSI_OPCODE_TXQS
||
63 opcode
== TGSI_OPCODE_TXQ_LZ
||
64 opcode
== TGSI_OPCODE_LODQ
;
68 * Is the opcode a "true" texture instruction which samples from a
72 is_texture_inst(unsigned opcode
)
74 return (!is_mem_query_inst(opcode
) &&
75 tgsi_get_opcode_info(opcode
)->is_tex
);
80 * Is the opcode an instruction which computes a derivative explicitly or
84 computes_derivative(unsigned opcode
)
86 if (tgsi_get_opcode_info(opcode
)->is_tex
) {
87 return opcode
!= TGSI_OPCODE_TG4
&&
88 opcode
!= TGSI_OPCODE_TXD
&&
89 opcode
!= TGSI_OPCODE_TXF
&&
90 opcode
!= TGSI_OPCODE_TXL
&&
91 opcode
!= TGSI_OPCODE_TXL2
&&
92 opcode
!= TGSI_OPCODE_TXQ
&&
93 opcode
!= TGSI_OPCODE_TXQ_LZ
&&
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 /* Mark which inputs are effectively used */
117 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
118 if (src
->Register
.Indirect
) {
119 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
120 info
->input_usage_mask
[ind
] |= usage_mask
;
124 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
125 info
->input_usage_mask
[ind
] |= usage_mask
;
128 if (info
->processor
== PIPE_SHADER_FRAGMENT
) {
129 unsigned name
, index
, input
;
131 if (src
->Register
.Indirect
&& src
->Indirect
.ArrayID
)
132 input
= info
->input_array_first
[src
->Indirect
.ArrayID
];
134 input
= src
->Register
.Index
;
136 name
= info
->input_semantic_name
[input
];
137 index
= info
->input_semantic_index
[input
];
139 if (name
== TGSI_SEMANTIC_POSITION
&&
140 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
141 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
142 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
143 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
))
144 info
->reads_z
= TRUE
;
146 if (name
== TGSI_SEMANTIC_COLOR
) {
148 (1 << src
->Register
.SwizzleX
) |
149 (1 << src
->Register
.SwizzleY
) |
150 (1 << src
->Register
.SwizzleZ
) |
151 (1 << src
->Register
.SwizzleW
);
153 info
->colors_read
|= mask
<< (index
* 4);
156 /* Process only interpolated varyings. Don't include POSITION.
157 * Don't include integer varyings, because they are not
158 * interpolated. Don't process inputs interpolated by INTERP
159 * opcodes. Those are tracked separately.
161 if ((!is_interp_instruction
|| src_index
!= 0) &&
162 (name
== TGSI_SEMANTIC_GENERIC
||
163 name
== TGSI_SEMANTIC_TEXCOORD
||
164 name
== TGSI_SEMANTIC_COLOR
||
165 name
== TGSI_SEMANTIC_BCOLOR
||
166 name
== TGSI_SEMANTIC_FOG
||
167 name
== TGSI_SEMANTIC_CLIPDIST
)) {
168 switch (info
->input_interpolate
[input
]) {
169 case TGSI_INTERPOLATE_COLOR
:
170 case TGSI_INTERPOLATE_PERSPECTIVE
:
171 switch (info
->input_interpolate_loc
[input
]) {
172 case TGSI_INTERPOLATE_LOC_CENTER
:
173 info
->uses_persp_center
= TRUE
;
175 case TGSI_INTERPOLATE_LOC_CENTROID
:
176 info
->uses_persp_centroid
= TRUE
;
178 case TGSI_INTERPOLATE_LOC_SAMPLE
:
179 info
->uses_persp_sample
= TRUE
;
183 case TGSI_INTERPOLATE_LINEAR
:
184 switch (info
->input_interpolate_loc
[input
]) {
185 case TGSI_INTERPOLATE_LOC_CENTER
:
186 info
->uses_linear_center
= TRUE
;
188 case TGSI_INTERPOLATE_LOC_CENTROID
:
189 info
->uses_linear_centroid
= TRUE
;
191 case TGSI_INTERPOLATE_LOC_SAMPLE
:
192 info
->uses_linear_sample
= TRUE
;
196 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
202 /* check for indirect register reads */
203 if (src
->Register
.Indirect
) {
204 info
->indirect_files
|= (1 << src
->Register
.File
);
205 info
->indirect_files_read
|= (1 << src
->Register
.File
);
207 /* record indirect constant buffer indexing */
208 if (src
->Register
.File
== TGSI_FILE_CONSTANT
) {
209 if (src
->Register
.Dimension
) {
210 if (src
->Dimension
.Indirect
)
211 info
->const_buffers_indirect
= info
->const_buffers_declared
;
213 info
->const_buffers_indirect
|= 1u << src
->Dimension
.Index
;
215 info
->const_buffers_indirect
|= 1;
220 if (src
->Register
.Dimension
&& src
->Dimension
.Indirect
)
221 info
->dim_indirect_files
|= 1u << src
->Register
.File
;
223 /* Texture samplers */
224 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
225 const unsigned index
= src
->Register
.Index
;
227 assert(fullinst
->Instruction
.Texture
);
228 assert(index
< ARRAY_SIZE(info
->is_msaa_sampler
));
229 assert(index
< PIPE_MAX_SAMPLERS
);
231 if (is_texture_inst(fullinst
->Instruction
.Opcode
)) {
232 const unsigned target
= fullinst
->Texture
.Texture
;
233 assert(target
< TGSI_TEXTURE_UNKNOWN
);
234 /* for texture instructions, check that the texture instruction
235 * target matches the previous sampler view declaration (if there
238 if (info
->sampler_targets
[index
] == TGSI_TEXTURE_UNKNOWN
) {
239 /* probably no sampler view declaration */
240 info
->sampler_targets
[index
] = target
;
242 /* Make sure the texture instruction's sampler/target info
243 * agrees with the sampler view declaration.
245 assert(info
->sampler_targets
[index
] == target
);
248 if (target
== TGSI_TEXTURE_2D_MSAA
||
249 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
250 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
255 if (is_memory_file(src
->Register
.File
) &&
256 !is_mem_query_inst(fullinst
->Instruction
.Opcode
)) {
259 if (tgsi_get_opcode_info(fullinst
->Instruction
.Opcode
)->is_store
) {
260 info
->writes_memory
= TRUE
;
262 if (src
->Register
.File
== TGSI_FILE_IMAGE
) {
263 if (src
->Register
.Indirect
)
264 info
->images_writemask
= info
->images_declared
;
266 info
->images_writemask
|= 1 << src
->Register
.Index
;
267 } else if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
268 if (src
->Register
.Indirect
)
269 info
->shader_buffers_atomic
= info
->shader_buffers_declared
;
271 info
->shader_buffers_atomic
|= 1 << src
->Register
.Index
;
274 if (src
->Register
.File
== TGSI_FILE_BUFFER
) {
275 if (src
->Register
.Indirect
)
276 info
->shader_buffers_load
= info
->shader_buffers_declared
;
278 info
->shader_buffers_load
|= 1 << src
->Register
.Index
;
286 scan_instruction(struct tgsi_shader_info
*info
,
287 const struct tgsi_full_instruction
*fullinst
,
288 unsigned *current_depth
)
291 bool is_mem_inst
= false;
292 bool is_interp_instruction
= false;
294 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
295 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
297 switch (fullinst
->Instruction
.Opcode
) {
299 case TGSI_OPCODE_UIF
:
300 case TGSI_OPCODE_BGNLOOP
:
302 info
->max_depth
= MAX2(info
->max_depth
, *current_depth
);
304 case TGSI_OPCODE_ENDIF
:
305 case TGSI_OPCODE_ENDLOOP
:
312 if (fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_CENTROID
||
313 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
314 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
315 const struct tgsi_full_src_register
*src0
= &fullinst
->Src
[0];
318 is_interp_instruction
= true;
320 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
321 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
323 input
= src0
->Register
.Index
;
325 /* For the INTERP opcodes, the interpolation is always
326 * PERSPECTIVE unless LINEAR is specified.
328 switch (info
->input_interpolate
[input
]) {
329 case TGSI_INTERPOLATE_COLOR
:
330 case TGSI_INTERPOLATE_CONSTANT
:
331 case TGSI_INTERPOLATE_PERSPECTIVE
:
332 switch (fullinst
->Instruction
.Opcode
) {
333 case TGSI_OPCODE_INTERP_CENTROID
:
334 info
->uses_persp_opcode_interp_centroid
= TRUE
;
336 case TGSI_OPCODE_INTERP_OFFSET
:
337 info
->uses_persp_opcode_interp_offset
= TRUE
;
339 case TGSI_OPCODE_INTERP_SAMPLE
:
340 info
->uses_persp_opcode_interp_sample
= TRUE
;
345 case TGSI_INTERPOLATE_LINEAR
:
346 switch (fullinst
->Instruction
.Opcode
) {
347 case TGSI_OPCODE_INTERP_CENTROID
:
348 info
->uses_linear_opcode_interp_centroid
= TRUE
;
350 case TGSI_OPCODE_INTERP_OFFSET
:
351 info
->uses_linear_opcode_interp_offset
= TRUE
;
353 case TGSI_OPCODE_INTERP_SAMPLE
:
354 info
->uses_linear_opcode_interp_sample
= TRUE
;
361 if (fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
362 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
)
363 info
->uses_doubles
= TRUE
;
365 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
366 scan_src_operand(info
, fullinst
, &fullinst
->Src
[i
], i
,
367 tgsi_util_get_inst_usage_mask(fullinst
, i
),
368 is_interp_instruction
, &is_mem_inst
);
371 if (fullinst
->Instruction
.Texture
) {
372 for (i
= 0; i
< fullinst
->Texture
.NumOffsets
; i
++) {
373 struct tgsi_full_src_register src
= {{0}};
375 src
.Register
.File
= fullinst
->TexOffsets
[i
].File
;
376 src
.Register
.Index
= fullinst
->TexOffsets
[i
].Index
;
377 src
.Register
.SwizzleX
= fullinst
->TexOffsets
[i
].SwizzleX
;
378 src
.Register
.SwizzleY
= fullinst
->TexOffsets
[i
].SwizzleY
;
379 src
.Register
.SwizzleZ
= fullinst
->TexOffsets
[i
].SwizzleZ
;
381 /* The usage mask is suboptimal but should be safe. */
382 scan_src_operand(info
, fullinst
, &src
, 0, TGSI_WRITEMASK_XYZ
,
383 false, &is_mem_inst
);
387 /* check for indirect register writes */
388 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
389 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
390 if (dst
->Register
.Indirect
) {
391 info
->indirect_files
|= (1 << dst
->Register
.File
);
392 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
395 if (dst
->Register
.Dimension
&& dst
->Dimension
.Indirect
)
396 info
->dim_indirect_files
|= 1u << dst
->Register
.File
;
398 if (is_memory_file(dst
->Register
.File
)) {
399 assert(fullinst
->Instruction
.Opcode
== TGSI_OPCODE_STORE
);
402 info
->writes_memory
= TRUE
;
404 if (dst
->Register
.File
== TGSI_FILE_IMAGE
) {
405 if (dst
->Register
.Indirect
)
406 info
->images_writemask
= info
->images_declared
;
408 info
->images_writemask
|= 1 << dst
->Register
.Index
;
409 } else if (dst
->Register
.File
== TGSI_FILE_BUFFER
) {
410 if (dst
->Register
.Indirect
)
411 info
->shader_buffers_store
= info
->shader_buffers_declared
;
413 info
->shader_buffers_store
|= 1 << dst
->Register
.Index
;
419 info
->num_memory_instructions
++;
421 if (computes_derivative(fullinst
->Instruction
.Opcode
))
422 info
->uses_derivatives
= true;
424 info
->num_instructions
++;
429 scan_declaration(struct tgsi_shader_info
*info
,
430 const struct tgsi_full_declaration
*fulldecl
)
432 const uint file
= fulldecl
->Declaration
.File
;
433 const unsigned procType
= info
->processor
;
436 if (fulldecl
->Declaration
.Array
) {
437 unsigned array_id
= fulldecl
->Array
.ArrayID
;
440 case TGSI_FILE_INPUT
:
441 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
442 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
443 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
445 case TGSI_FILE_OUTPUT
:
446 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
447 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
448 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
451 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
454 for (reg
= fulldecl
->Range
.First
; reg
<= fulldecl
->Range
.Last
; reg
++) {
455 unsigned semName
= fulldecl
->Semantic
.Name
;
456 unsigned semIndex
= fulldecl
->Semantic
.Index
+
457 (reg
- fulldecl
->Range
.First
);
459 unsigned index
, target
, type
;
461 /* only first 32 regs will appear in this bitfield */
462 info
->file_mask
[file
] |= (1 << reg
);
463 info
->file_count
[file
]++;
464 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
467 case TGSI_FILE_CONSTANT
:
470 if (fulldecl
->Declaration
.Dimension
)
471 buffer
= fulldecl
->Dim
.Index2D
;
473 info
->const_file_max
[buffer
] =
474 MAX2(info
->const_file_max
[buffer
], (int)reg
);
475 info
->const_buffers_declared
|= 1u << buffer
;
478 case TGSI_FILE_IMAGE
:
479 info
->images_declared
|= 1u << reg
;
480 if (fulldecl
->Image
.Resource
== TGSI_TEXTURE_BUFFER
)
481 info
->images_buffers
|= 1 << reg
;
484 case TGSI_FILE_BUFFER
:
485 info
->shader_buffers_declared
|= 1u << reg
;
488 case TGSI_FILE_INPUT
:
489 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
490 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
491 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
492 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
493 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
495 /* Vertex shaders can have inputs with holes between them. */
496 info
->num_inputs
= MAX2(info
->num_inputs
, reg
+ 1);
498 if (semName
== TGSI_SEMANTIC_PRIMID
)
499 info
->uses_primid
= TRUE
;
500 else if (procType
== PIPE_SHADER_FRAGMENT
) {
501 if (semName
== TGSI_SEMANTIC_POSITION
)
502 info
->reads_position
= TRUE
;
503 else if (semName
== TGSI_SEMANTIC_FACE
)
504 info
->uses_frontface
= TRUE
;
508 case TGSI_FILE_SYSTEM_VALUE
:
509 index
= fulldecl
->Range
.First
;
511 info
->system_value_semantic_name
[index
] = semName
;
512 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
515 case TGSI_SEMANTIC_INSTANCEID
:
516 info
->uses_instanceid
= TRUE
;
518 case TGSI_SEMANTIC_VERTEXID
:
519 info
->uses_vertexid
= TRUE
;
521 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
522 info
->uses_vertexid_nobase
= TRUE
;
524 case TGSI_SEMANTIC_BASEVERTEX
:
525 info
->uses_basevertex
= TRUE
;
527 case TGSI_SEMANTIC_PRIMID
:
528 info
->uses_primid
= TRUE
;
530 case TGSI_SEMANTIC_INVOCATIONID
:
531 info
->uses_invocationid
= TRUE
;
533 case TGSI_SEMANTIC_POSITION
:
534 info
->reads_position
= TRUE
;
536 case TGSI_SEMANTIC_FACE
:
537 info
->uses_frontface
= TRUE
;
539 case TGSI_SEMANTIC_SAMPLEMASK
:
540 info
->reads_samplemask
= TRUE
;
545 case TGSI_FILE_OUTPUT
:
546 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
547 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
548 info
->output_usagemask
[reg
] |= fulldecl
->Declaration
.UsageMask
;
549 info
->num_outputs
= MAX2(info
->num_outputs
, reg
+ 1);
551 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_X
) {
552 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamX
;
553 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamX
]++;
555 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Y
) {
556 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamY
<< 2;
557 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamY
]++;
559 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_Z
) {
560 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamZ
<< 4;
561 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamZ
]++;
563 if (fulldecl
->Declaration
.UsageMask
& TGSI_WRITEMASK_W
) {
564 info
->output_streams
[reg
] |= (ubyte
)fulldecl
->Semantic
.StreamW
<< 6;
565 info
->num_stream_output_components
[fulldecl
->Semantic
.StreamW
]++;
569 case TGSI_SEMANTIC_PRIMID
:
570 info
->writes_primid
= true;
572 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
573 info
->writes_viewport_index
= true;
575 case TGSI_SEMANTIC_LAYER
:
576 info
->writes_layer
= true;
578 case TGSI_SEMANTIC_PSIZE
:
579 info
->writes_psize
= true;
581 case TGSI_SEMANTIC_CLIPVERTEX
:
582 info
->writes_clipvertex
= true;
584 case TGSI_SEMANTIC_COLOR
:
585 info
->colors_written
|= 1 << semIndex
;
587 case TGSI_SEMANTIC_STENCIL
:
588 info
->writes_stencil
= true;
590 case TGSI_SEMANTIC_SAMPLEMASK
:
591 info
->writes_samplemask
= true;
593 case TGSI_SEMANTIC_EDGEFLAG
:
594 info
->writes_edgeflag
= true;
596 case TGSI_SEMANTIC_POSITION
:
597 if (procType
== PIPE_SHADER_FRAGMENT
)
598 info
->writes_z
= true;
600 info
->writes_position
= true;
605 case TGSI_FILE_SAMPLER
:
606 STATIC_ASSERT(sizeof(info
->samplers_declared
) * 8 >= PIPE_MAX_SAMPLERS
);
607 info
->samplers_declared
|= 1u << reg
;
610 case TGSI_FILE_SAMPLER_VIEW
:
611 target
= fulldecl
->SamplerView
.Resource
;
612 type
= fulldecl
->SamplerView
.ReturnTypeX
;
614 assert(target
< TGSI_TEXTURE_UNKNOWN
);
615 if (info
->sampler_targets
[reg
] == TGSI_TEXTURE_UNKNOWN
) {
616 /* Save sampler target for this sampler index */
617 info
->sampler_targets
[reg
] = target
;
618 info
->sampler_type
[reg
] = type
;
620 /* if previously declared, make sure targets agree */
621 assert(info
->sampler_targets
[reg
] == target
);
622 assert(info
->sampler_type
[reg
] == type
);
631 scan_immediate(struct tgsi_shader_info
*info
)
633 uint reg
= info
->immediate_count
++;
634 uint file
= TGSI_FILE_IMMEDIATE
;
636 info
->file_mask
[file
] |= (1 << reg
);
637 info
->file_count
[file
]++;
638 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
643 scan_property(struct tgsi_shader_info
*info
,
644 const struct tgsi_full_property
*fullprop
)
646 unsigned name
= fullprop
->Property
.PropertyName
;
647 unsigned value
= fullprop
->u
[0].Data
;
649 assert(name
< ARRAY_SIZE(info
->properties
));
650 info
->properties
[name
] = value
;
653 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
654 info
->num_written_clipdistance
= value
;
655 info
->clipdist_writemask
|= (1 << value
) - 1;
657 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
658 info
->num_written_culldistance
= value
;
659 info
->culldist_writemask
|= (1 << value
) - 1;
666 * Scan the given TGSI shader to collect information such as number of
667 * registers used, special instructions used, etc.
668 * \return info the result of the scan
671 tgsi_scan_shader(const struct tgsi_token
*tokens
,
672 struct tgsi_shader_info
*info
)
675 struct tgsi_parse_context parse
;
676 unsigned current_depth
= 0;
678 memset(info
, 0, sizeof(*info
));
679 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
680 info
->file_max
[i
] = -1;
681 for (i
= 0; i
< ARRAY_SIZE(info
->const_file_max
); i
++)
682 info
->const_file_max
[i
] = -1;
683 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
684 for (i
= 0; i
< ARRAY_SIZE(info
->sampler_targets
); i
++)
685 info
->sampler_targets
[i
] = TGSI_TEXTURE_UNKNOWN
;
688 ** Setup to begin parsing input shader
690 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
691 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
694 procType
= parse
.FullHeader
.Processor
.Processor
;
695 assert(procType
== PIPE_SHADER_FRAGMENT
||
696 procType
== PIPE_SHADER_VERTEX
||
697 procType
== PIPE_SHADER_GEOMETRY
||
698 procType
== PIPE_SHADER_TESS_CTRL
||
699 procType
== PIPE_SHADER_TESS_EVAL
||
700 procType
== PIPE_SHADER_COMPUTE
);
701 info
->processor
= procType
;
704 ** Loop over incoming program tokens/instructions
706 while (!tgsi_parse_end_of_tokens(&parse
)) {
709 tgsi_parse_token( &parse
);
711 switch( parse
.FullToken
.Token
.Type
) {
712 case TGSI_TOKEN_TYPE_INSTRUCTION
:
713 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
716 case TGSI_TOKEN_TYPE_DECLARATION
:
717 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
719 case TGSI_TOKEN_TYPE_IMMEDIATE
:
720 scan_immediate(info
);
722 case TGSI_TOKEN_TYPE_PROPERTY
:
723 scan_property(info
, &parse
.FullToken
.FullProperty
);
726 assert(!"Unexpected TGSI token type");
730 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
731 info
->opcode_count
[TGSI_OPCODE_KILL
]);
733 /* The dimensions of the IN decleration in geometry shader have
734 * to be deduced from the type of the input primitive.
736 if (procType
== PIPE_SHADER_GEOMETRY
) {
737 unsigned input_primitive
=
738 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
739 int num_verts
= u_vertices_per_prim(input_primitive
);
741 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
742 info
->file_max
[TGSI_FILE_INPUT
] =
743 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
744 for (j
= 0; j
< num_verts
; ++j
) {
745 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
749 tgsi_parse_free(&parse
);
753 * Collect information about the arrays of a given register file.
755 * @param tokens TGSI shader
756 * @param file the register file to scan through
757 * @param max_array_id number of entries in @p arrays; should be equal to the
758 * highest array id, i.e. tgsi_shader_info::array_max[file].
759 * @param arrays info for array of each ID will be written to arrays[ID - 1].
762 tgsi_scan_arrays(const struct tgsi_token
*tokens
,
764 unsigned max_array_id
,
765 struct tgsi_array_info
*arrays
)
767 struct tgsi_parse_context parse
;
769 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
770 debug_printf("tgsi_parse_init() failed in tgsi_scan_arrays()!\n");
774 memset(arrays
, 0, sizeof(arrays
[0]) * max_array_id
);
776 while (!tgsi_parse_end_of_tokens(&parse
)) {
777 struct tgsi_full_instruction
*inst
;
779 tgsi_parse_token(&parse
);
781 if (parse
.FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
782 struct tgsi_full_declaration
*decl
= &parse
.FullToken
.FullDeclaration
;
784 if (decl
->Declaration
.Array
&& decl
->Declaration
.File
== file
&&
785 decl
->Array
.ArrayID
> 0 && decl
->Array
.ArrayID
<= max_array_id
) {
786 struct tgsi_array_info
*array
= &arrays
[decl
->Array
.ArrayID
- 1];
787 assert(!array
->declared
);
788 array
->declared
= true;
789 array
->range
= decl
->Range
;
793 if (parse
.FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_INSTRUCTION
)
796 inst
= &parse
.FullToken
.FullInstruction
;
797 for (unsigned i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
798 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
799 if (dst
->Register
.File
!= file
)
802 if (dst
->Register
.Indirect
) {
803 if (dst
->Indirect
.ArrayID
> 0 &&
804 dst
->Indirect
.ArrayID
<= max_array_id
) {
805 arrays
[dst
->Indirect
.ArrayID
- 1].writemask
|= dst
->Register
.WriteMask
;
807 /* Indirect writes without an ArrayID can write anywhere. */
808 for (unsigned j
= 0; j
< max_array_id
; ++j
)
809 arrays
[j
].writemask
|= dst
->Register
.WriteMask
;
812 /* Check whether the write falls into any of the arrays anyway. */
813 for (unsigned j
= 0; j
< max_array_id
; ++j
) {
814 struct tgsi_array_info
*array
= &arrays
[j
];
815 if (array
->declared
&&
816 dst
->Register
.Index
>= array
->range
.First
&&
817 dst
->Register
.Index
<= array
->range
.Last
)
818 array
->writemask
|= dst
->Register
.WriteMask
;
824 tgsi_parse_free(&parse
);
831 * Check if the given shader is a "passthrough" shader consisting of only
832 * MOV instructions of the form: MOV OUT[n], IN[n]
836 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
838 struct tgsi_parse_context parse
;
841 ** Setup to begin parsing input shader
843 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
844 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
849 ** Loop over incoming program tokens/instructions
851 while (!tgsi_parse_end_of_tokens(&parse
)) {
853 tgsi_parse_token(&parse
);
855 switch (parse
.FullToken
.Token
.Type
) {
856 case TGSI_TOKEN_TYPE_INSTRUCTION
:
858 struct tgsi_full_instruction
*fullinst
=
859 &parse
.FullToken
.FullInstruction
;
860 const struct tgsi_full_src_register
*src
=
862 const struct tgsi_full_dst_register
*dst
=
865 /* Do a whole bunch of checks for a simple move */
866 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
867 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
868 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
869 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
870 src
->Register
.Index
!= dst
->Register
.Index
||
872 src
->Register
.Negate
||
873 src
->Register
.Absolute
||
875 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
876 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
877 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
878 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
880 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
882 tgsi_parse_free(&parse
);
888 case TGSI_TOKEN_TYPE_DECLARATION
:
890 case TGSI_TOKEN_TYPE_IMMEDIATE
:
892 case TGSI_TOKEN_TYPE_PROPERTY
:
899 tgsi_parse_free(&parse
);
901 /* if we get here, it's a pass-through shader */