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 if (fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
104 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
)
105 info
->uses_doubles
= true;
107 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
108 const struct tgsi_full_src_register
*src
=
110 int ind
= src
->Register
.Index
;
112 /* Mark which inputs are effectively used */
113 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
115 usage_mask
= tgsi_util_get_inst_usage_mask(fullinst
, i
);
116 if (src
->Register
.Indirect
) {
117 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
118 info
->input_usage_mask
[ind
] |= usage_mask
;
122 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
123 info
->input_usage_mask
[ind
] |= usage_mask
;
126 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
127 info
->reads_position
&&
128 src
->Register
.Index
== 0 &&
129 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
130 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
131 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
132 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
)) {
133 info
->reads_z
= TRUE
;
137 /* check for indirect register reads */
138 if (src
->Register
.Indirect
) {
139 info
->indirect_files
|= (1 << src
->Register
.File
);
140 info
->indirect_files_read
|= (1 << src
->Register
.File
);
144 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
145 assert(fullinst
->Instruction
.Texture
);
146 assert(src
->Register
.Index
< Elements(info
->is_msaa_sampler
));
148 if (fullinst
->Instruction
.Texture
&&
149 (fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_MSAA
||
150 fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_ARRAY_MSAA
)) {
151 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
156 /* check for indirect register writes */
157 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
158 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
159 if (dst
->Register
.Indirect
) {
160 info
->indirect_files
|= (1 << dst
->Register
.File
);
161 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
165 info
->num_instructions
++;
169 case TGSI_TOKEN_TYPE_DECLARATION
:
171 const struct tgsi_full_declaration
*fulldecl
172 = &parse
.FullToken
.FullDeclaration
;
173 const uint file
= fulldecl
->Declaration
.File
;
176 if (fulldecl
->Declaration
.Array
) {
177 unsigned array_id
= fulldecl
->Array
.ArrayID
;
180 case TGSI_FILE_INPUT
:
181 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
182 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
183 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
185 case TGSI_FILE_OUTPUT
:
186 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
187 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
188 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
191 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
194 for (reg
= fulldecl
->Range
.First
;
195 reg
<= fulldecl
->Range
.Last
;
197 unsigned semName
= fulldecl
->Semantic
.Name
;
199 fulldecl
->Semantic
.Index
+ (reg
- fulldecl
->Range
.First
);
201 /* only first 32 regs will appear in this bitfield */
202 info
->file_mask
[file
] |= (1 << reg
);
203 info
->file_count
[file
]++;
204 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
206 if (file
== TGSI_FILE_CONSTANT
) {
209 if (fulldecl
->Declaration
.Dimension
)
210 buffer
= fulldecl
->Dim
.Index2D
;
212 info
->const_file_max
[buffer
] =
213 MAX2(info
->const_file_max
[buffer
], (int)reg
);
215 else if (file
== TGSI_FILE_INPUT
) {
216 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
217 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
218 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
219 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
220 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
223 if (fulldecl
->Interp
.Location
== TGSI_INTERPOLATE_LOC_CENTROID
)
224 info
->uses_centroid
= TRUE
;
226 if (semName
== TGSI_SEMANTIC_PRIMID
)
227 info
->uses_primid
= TRUE
;
228 else if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
229 if (semName
== TGSI_SEMANTIC_POSITION
)
230 info
->reads_position
= TRUE
;
231 else if (semName
== TGSI_SEMANTIC_FACE
)
232 info
->uses_frontface
= TRUE
;
235 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
236 unsigned index
= fulldecl
->Range
.First
;
238 info
->system_value_semantic_name
[index
] = semName
;
239 info
->num_system_values
= MAX2(info
->num_system_values
,
242 if (semName
== TGSI_SEMANTIC_INSTANCEID
) {
243 info
->uses_instanceid
= TRUE
;
245 else if (semName
== TGSI_SEMANTIC_VERTEXID
) {
246 info
->uses_vertexid
= TRUE
;
248 else if (semName
== TGSI_SEMANTIC_VERTEXID_NOBASE
) {
249 info
->uses_vertexid_nobase
= TRUE
;
251 else if (semName
== TGSI_SEMANTIC_BASEVERTEX
) {
252 info
->uses_basevertex
= TRUE
;
254 else if (semName
== TGSI_SEMANTIC_PRIMID
) {
255 info
->uses_primid
= TRUE
;
256 } else if (semName
== TGSI_SEMANTIC_INVOCATIONID
) {
257 info
->uses_invocationid
= TRUE
;
260 else if (file
== TGSI_FILE_OUTPUT
) {
261 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
262 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
265 if (procType
== TGSI_PROCESSOR_VERTEX
||
266 procType
== TGSI_PROCESSOR_GEOMETRY
||
267 procType
== TGSI_PROCESSOR_TESS_CTRL
||
268 procType
== TGSI_PROCESSOR_TESS_EVAL
) {
269 if (semName
== TGSI_SEMANTIC_CLIPDIST
) {
270 info
->num_written_clipdistance
+=
271 util_bitcount(fulldecl
->Declaration
.UsageMask
);
272 info
->clipdist_writemask
|=
273 fulldecl
->Declaration
.UsageMask
<< (semIndex
*4);
275 else if (semName
== TGSI_SEMANTIC_CULLDIST
) {
276 info
->num_written_culldistance
+=
277 util_bitcount(fulldecl
->Declaration
.UsageMask
);
278 info
->culldist_writemask
|=
279 fulldecl
->Declaration
.UsageMask
<< (semIndex
*4);
281 else if (semName
== TGSI_SEMANTIC_VIEWPORT_INDEX
) {
282 info
->writes_viewport_index
= TRUE
;
284 else if (semName
== TGSI_SEMANTIC_LAYER
) {
285 info
->writes_layer
= TRUE
;
287 else if (semName
== TGSI_SEMANTIC_PSIZE
) {
288 info
->writes_psize
= TRUE
;
290 else if (semName
== TGSI_SEMANTIC_CLIPVERTEX
) {
291 info
->writes_clipvertex
= TRUE
;
295 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
296 if (semName
== TGSI_SEMANTIC_POSITION
) {
297 info
->writes_z
= TRUE
;
299 else if (semName
== TGSI_SEMANTIC_STENCIL
) {
300 info
->writes_stencil
= TRUE
;
304 if (procType
== TGSI_PROCESSOR_VERTEX
) {
305 if (semName
== TGSI_SEMANTIC_EDGEFLAG
) {
306 info
->writes_edgeflag
= TRUE
;
314 case TGSI_TOKEN_TYPE_IMMEDIATE
:
316 uint reg
= info
->immediate_count
++;
317 uint file
= TGSI_FILE_IMMEDIATE
;
319 info
->file_mask
[file
] |= (1 << reg
);
320 info
->file_count
[file
]++;
321 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
325 case TGSI_TOKEN_TYPE_PROPERTY
:
327 const struct tgsi_full_property
*fullprop
328 = &parse
.FullToken
.FullProperty
;
329 unsigned name
= fullprop
->Property
.PropertyName
;
331 assert(name
< Elements(info
->properties
));
332 info
->properties
[name
] = fullprop
->u
[0].Data
;
341 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
342 info
->opcode_count
[TGSI_OPCODE_KILL
]);
344 /* The dimensions of the IN decleration in geometry shader have
345 * to be deduced from the type of the input primitive.
347 if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
348 unsigned input_primitive
=
349 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
350 int num_verts
= u_vertices_per_prim(input_primitive
);
352 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
353 info
->file_max
[TGSI_FILE_INPUT
] =
354 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
355 for (j
= 0; j
< num_verts
; ++j
) {
356 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
360 tgsi_parse_free (&parse
);
366 * Check if the given shader is a "passthrough" shader consisting of only
367 * MOV instructions of the form: MOV OUT[n], IN[n]
371 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
373 struct tgsi_parse_context parse
;
376 ** Setup to begin parsing input shader
378 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
379 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
384 ** Loop over incoming program tokens/instructions
386 while (!tgsi_parse_end_of_tokens(&parse
)) {
388 tgsi_parse_token(&parse
);
390 switch (parse
.FullToken
.Token
.Type
) {
391 case TGSI_TOKEN_TYPE_INSTRUCTION
:
393 struct tgsi_full_instruction
*fullinst
=
394 &parse
.FullToken
.FullInstruction
;
395 const struct tgsi_full_src_register
*src
=
397 const struct tgsi_full_dst_register
*dst
=
400 /* Do a whole bunch of checks for a simple move */
401 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
402 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
403 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
404 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
405 src
->Register
.Index
!= dst
->Register
.Index
||
407 src
->Register
.Negate
||
408 src
->Register
.Absolute
||
410 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
411 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
412 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
413 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
415 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
417 tgsi_parse_free(&parse
);
423 case TGSI_TOKEN_TYPE_DECLARATION
:
425 case TGSI_TOKEN_TYPE_IMMEDIATE
:
427 case TGSI_TOKEN_TYPE_PROPERTY
:
434 tgsi_parse_free(&parse
);
436 /* if we get here, it's a pass-through shader */