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_parse.h"
42 #include "tgsi/tgsi_util.h"
43 #include "tgsi/tgsi_scan.h"
49 * Scan the given TGSI shader to collect information such as number of
50 * registers used, special instructions used, etc.
51 * \return info the result of the scan
54 tgsi_scan_shader(const struct tgsi_token
*tokens
,
55 struct tgsi_shader_info
*info
)
58 struct tgsi_parse_context parse
;
60 memset(info
, 0, sizeof(*info
));
61 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
62 info
->file_max
[i
] = -1;
63 for (i
= 0; i
< Elements(info
->const_file_max
); i
++)
64 info
->const_file_max
[i
] = -1;
65 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
68 ** Setup to begin parsing input shader
70 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
71 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
74 procType
= parse
.FullHeader
.Processor
.Processor
;
75 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
76 procType
== TGSI_PROCESSOR_VERTEX
||
77 procType
== TGSI_PROCESSOR_GEOMETRY
||
78 procType
== TGSI_PROCESSOR_TESS_CTRL
||
79 procType
== TGSI_PROCESSOR_TESS_EVAL
||
80 procType
== TGSI_PROCESSOR_COMPUTE
);
81 info
->processor
= procType
;
85 ** Loop over incoming program tokens/instructions
87 while( !tgsi_parse_end_of_tokens( &parse
) ) {
91 tgsi_parse_token( &parse
);
93 switch( parse
.FullToken
.Token
.Type
) {
94 case TGSI_TOKEN_TYPE_INSTRUCTION
:
96 const struct tgsi_full_instruction
*fullinst
97 = &parse
.FullToken
.FullInstruction
;
100 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
101 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
103 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
104 const struct tgsi_full_src_register
*src
=
106 int ind
= src
->Register
.Index
;
108 /* Mark which inputs are effectively used */
109 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
111 usage_mask
= tgsi_util_get_inst_usage_mask(fullinst
, i
);
112 if (src
->Register
.Indirect
) {
113 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
114 info
->input_usage_mask
[ind
] |= usage_mask
;
118 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
119 info
->input_usage_mask
[ind
] |= usage_mask
;
122 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
123 info
->reads_position
&&
124 src
->Register
.Index
== 0 &&
125 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
126 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
127 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
128 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
)) {
129 info
->reads_z
= TRUE
;
133 /* check for indirect register reads */
134 if (src
->Register
.Indirect
) {
135 info
->indirect_files
|= (1 << src
->Register
.File
);
136 info
->indirect_files_read
|= (1 << src
->Register
.File
);
140 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
141 assert(fullinst
->Instruction
.Texture
);
142 assert(src
->Register
.Index
< Elements(info
->is_msaa_sampler
));
144 if (fullinst
->Instruction
.Texture
&&
145 (fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_MSAA
||
146 fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_ARRAY_MSAA
)) {
147 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
152 /* check for indirect register writes */
153 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
154 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
155 if (dst
->Register
.Indirect
) {
156 info
->indirect_files
|= (1 << dst
->Register
.File
);
157 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
161 info
->num_instructions
++;
165 case TGSI_TOKEN_TYPE_DECLARATION
:
167 const struct tgsi_full_declaration
*fulldecl
168 = &parse
.FullToken
.FullDeclaration
;
169 const uint file
= fulldecl
->Declaration
.File
;
172 if (fulldecl
->Declaration
.Array
) {
173 unsigned array_id
= fulldecl
->Array
.ArrayID
;
176 case TGSI_FILE_INPUT
:
177 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
178 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
179 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
181 case TGSI_FILE_OUTPUT
:
182 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
183 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
184 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
187 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
190 for (reg
= fulldecl
->Range
.First
;
191 reg
<= fulldecl
->Range
.Last
;
193 unsigned semName
= fulldecl
->Semantic
.Name
;
195 fulldecl
->Semantic
.Index
+ (reg
- fulldecl
->Range
.First
);
197 /* only first 32 regs will appear in this bitfield */
198 info
->file_mask
[file
] |= (1 << reg
);
199 info
->file_count
[file
]++;
200 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
202 if (file
== TGSI_FILE_CONSTANT
) {
205 if (fulldecl
->Declaration
.Dimension
)
206 buffer
= fulldecl
->Dim
.Index2D
;
208 info
->const_file_max
[buffer
] =
209 MAX2(info
->const_file_max
[buffer
], (int)reg
);
211 else if (file
== TGSI_FILE_INPUT
) {
212 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
213 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
214 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
215 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
216 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
219 if (fulldecl
->Interp
.Location
== TGSI_INTERPOLATE_LOC_CENTROID
)
220 info
->uses_centroid
= TRUE
;
222 if (semName
== TGSI_SEMANTIC_PRIMID
)
223 info
->uses_primid
= TRUE
;
224 else if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
225 if (semName
== TGSI_SEMANTIC_POSITION
)
226 info
->reads_position
= TRUE
;
227 else if (semName
== TGSI_SEMANTIC_FACE
)
228 info
->uses_frontface
= TRUE
;
231 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
232 unsigned index
= fulldecl
->Range
.First
;
234 info
->system_value_semantic_name
[index
] = semName
;
235 info
->num_system_values
= MAX2(info
->num_system_values
,
238 if (semName
== TGSI_SEMANTIC_INSTANCEID
) {
239 info
->uses_instanceid
= TRUE
;
241 else if (semName
== TGSI_SEMANTIC_VERTEXID
) {
242 info
->uses_vertexid
= TRUE
;
244 else if (semName
== TGSI_SEMANTIC_VERTEXID_NOBASE
) {
245 info
->uses_vertexid_nobase
= TRUE
;
247 else if (semName
== TGSI_SEMANTIC_BASEVERTEX
) {
248 info
->uses_basevertex
= TRUE
;
250 else if (semName
== TGSI_SEMANTIC_PRIMID
) {
251 info
->uses_primid
= TRUE
;
252 } else if (semName
== TGSI_SEMANTIC_INVOCATIONID
) {
253 info
->uses_invocationid
= TRUE
;
256 else if (file
== TGSI_FILE_OUTPUT
) {
257 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
258 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
261 if (procType
== TGSI_PROCESSOR_VERTEX
||
262 procType
== TGSI_PROCESSOR_GEOMETRY
||
263 procType
== TGSI_PROCESSOR_TESS_CTRL
||
264 procType
== TGSI_PROCESSOR_TESS_EVAL
) {
265 if (semName
== TGSI_SEMANTIC_CLIPDIST
) {
266 info
->num_written_clipdistance
+=
267 util_bitcount(fulldecl
->Declaration
.UsageMask
);
268 info
->clipdist_writemask
|=
269 fulldecl
->Declaration
.UsageMask
<< (semIndex
*4);
271 else if (semName
== TGSI_SEMANTIC_CULLDIST
) {
272 info
->num_written_culldistance
+=
273 util_bitcount(fulldecl
->Declaration
.UsageMask
);
274 info
->culldist_writemask
|=
275 fulldecl
->Declaration
.UsageMask
<< (semIndex
*4);
277 else if (semName
== TGSI_SEMANTIC_VIEWPORT_INDEX
) {
278 info
->writes_viewport_index
= TRUE
;
280 else if (semName
== TGSI_SEMANTIC_LAYER
) {
281 info
->writes_layer
= TRUE
;
283 else if (semName
== TGSI_SEMANTIC_PSIZE
) {
284 info
->writes_psize
= TRUE
;
286 else if (semName
== TGSI_SEMANTIC_CLIPVERTEX
) {
287 info
->writes_clipvertex
= TRUE
;
291 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
292 if (semName
== TGSI_SEMANTIC_POSITION
) {
293 info
->writes_z
= TRUE
;
295 else if (semName
== TGSI_SEMANTIC_STENCIL
) {
296 info
->writes_stencil
= TRUE
;
300 if (procType
== TGSI_PROCESSOR_VERTEX
) {
301 if (semName
== TGSI_SEMANTIC_EDGEFLAG
) {
302 info
->writes_edgeflag
= TRUE
;
310 case TGSI_TOKEN_TYPE_IMMEDIATE
:
312 uint reg
= info
->immediate_count
++;
313 uint file
= TGSI_FILE_IMMEDIATE
;
315 info
->file_mask
[file
] |= (1 << reg
);
316 info
->file_count
[file
]++;
317 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
321 case TGSI_TOKEN_TYPE_PROPERTY
:
323 const struct tgsi_full_property
*fullprop
324 = &parse
.FullToken
.FullProperty
;
325 unsigned name
= fullprop
->Property
.PropertyName
;
327 assert(name
< Elements(info
->properties
));
328 info
->properties
[name
] = fullprop
->u
[0].Data
;
337 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
338 info
->opcode_count
[TGSI_OPCODE_KILL
]);
340 /* The dimensions of the IN decleration in geometry shader have
341 * to be deduced from the type of the input primitive.
343 if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
344 unsigned input_primitive
=
345 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
346 int num_verts
= u_vertices_per_prim(input_primitive
);
348 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
349 info
->file_max
[TGSI_FILE_INPUT
] =
350 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
351 for (j
= 0; j
< num_verts
; ++j
) {
352 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
356 tgsi_parse_free (&parse
);
362 * Check if the given shader is a "passthrough" shader consisting of only
363 * MOV instructions of the form: MOV OUT[n], IN[n]
367 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
369 struct tgsi_parse_context parse
;
372 ** Setup to begin parsing input shader
374 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
375 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
380 ** Loop over incoming program tokens/instructions
382 while (!tgsi_parse_end_of_tokens(&parse
)) {
384 tgsi_parse_token(&parse
);
386 switch (parse
.FullToken
.Token
.Type
) {
387 case TGSI_TOKEN_TYPE_INSTRUCTION
:
389 struct tgsi_full_instruction
*fullinst
=
390 &parse
.FullToken
.FullInstruction
;
391 const struct tgsi_full_src_register
*src
=
393 const struct tgsi_full_dst_register
*dst
=
396 /* Do a whole bunch of checks for a simple move */
397 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
398 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
399 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
400 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
401 src
->Register
.Index
!= dst
->Register
.Index
||
403 src
->Register
.Negate
||
404 src
->Register
.Absolute
||
406 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
407 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
408 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
409 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
411 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
413 tgsi_parse_free(&parse
);
419 case TGSI_TOKEN_TYPE_DECLARATION
:
421 case TGSI_TOKEN_TYPE_IMMEDIATE
:
423 case TGSI_TOKEN_TYPE_PROPERTY
:
430 tgsi_parse_free(&parse
);
432 /* if we get here, it's a pass-through shader */