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;
67 ** Setup to begin parsing input shader
69 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
70 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
73 procType
= parse
.FullHeader
.Processor
.Processor
;
74 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
75 procType
== TGSI_PROCESSOR_VERTEX
||
76 procType
== TGSI_PROCESSOR_GEOMETRY
||
77 procType
== TGSI_PROCESSOR_TESS_CTRL
||
78 procType
== TGSI_PROCESSOR_TESS_EVAL
||
79 procType
== TGSI_PROCESSOR_COMPUTE
);
80 info
->processor
= procType
;
84 ** Loop over incoming program tokens/instructions
86 while( !tgsi_parse_end_of_tokens( &parse
) ) {
90 tgsi_parse_token( &parse
);
92 switch( parse
.FullToken
.Token
.Type
) {
93 case TGSI_TOKEN_TYPE_INSTRUCTION
:
95 const struct tgsi_full_instruction
*fullinst
96 = &parse
.FullToken
.FullInstruction
;
99 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
100 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
102 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
103 const struct tgsi_full_src_register
*src
=
105 int ind
= src
->Register
.Index
;
107 /* Mark which inputs are effectively used */
108 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
110 usage_mask
= tgsi_util_get_inst_usage_mask(fullinst
, i
);
111 if (src
->Register
.Indirect
) {
112 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
113 info
->input_usage_mask
[ind
] |= usage_mask
;
117 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
118 info
->input_usage_mask
[ind
] |= usage_mask
;
121 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
122 info
->reads_position
&&
123 src
->Register
.Index
== 0 &&
124 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
125 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
126 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
127 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
)) {
128 info
->reads_z
= TRUE
;
132 /* check for indirect register reads */
133 if (src
->Register
.Indirect
) {
134 info
->indirect_files
|= (1 << src
->Register
.File
);
135 info
->indirect_files_read
|= (1 << src
->Register
.File
);
139 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
140 assert(fullinst
->Instruction
.Texture
);
141 assert(src
->Register
.Index
< Elements(info
->is_msaa_sampler
));
143 if (fullinst
->Instruction
.Texture
&&
144 (fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_MSAA
||
145 fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_ARRAY_MSAA
)) {
146 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
151 /* check for indirect register writes */
152 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
153 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
154 if (dst
->Register
.Indirect
) {
155 info
->indirect_files
|= (1 << dst
->Register
.File
);
156 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
160 info
->num_instructions
++;
164 case TGSI_TOKEN_TYPE_DECLARATION
:
166 const struct tgsi_full_declaration
*fulldecl
167 = &parse
.FullToken
.FullDeclaration
;
168 const uint file
= fulldecl
->Declaration
.File
;
171 if (fulldecl
->Declaration
.Array
) {
172 unsigned array_id
= fulldecl
->Array
.ArrayID
;
175 case TGSI_FILE_INPUT
:
176 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
177 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
178 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
180 case TGSI_FILE_OUTPUT
:
181 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
182 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
183 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
186 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
189 for (reg
= fulldecl
->Range
.First
;
190 reg
<= fulldecl
->Range
.Last
;
192 unsigned semName
= fulldecl
->Semantic
.Name
;
194 fulldecl
->Semantic
.Index
+ (reg
- fulldecl
->Range
.First
);
196 /* only first 32 regs will appear in this bitfield */
197 info
->file_mask
[file
] |= (1 << reg
);
198 info
->file_count
[file
]++;
199 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
201 if (file
== TGSI_FILE_CONSTANT
) {
204 if (fulldecl
->Declaration
.Dimension
)
205 buffer
= fulldecl
->Dim
.Index2D
;
207 info
->const_file_max
[buffer
] =
208 MAX2(info
->const_file_max
[buffer
], (int)reg
);
210 else if (file
== TGSI_FILE_INPUT
) {
211 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
212 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
213 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
214 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
215 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
218 if (fulldecl
->Interp
.Location
== TGSI_INTERPOLATE_LOC_CENTROID
)
219 info
->uses_centroid
= TRUE
;
221 if (semName
== TGSI_SEMANTIC_PRIMID
)
222 info
->uses_primid
= TRUE
;
223 else if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
224 if (semName
== TGSI_SEMANTIC_POSITION
)
225 info
->reads_position
= TRUE
;
226 else if (semName
== TGSI_SEMANTIC_FACE
)
227 info
->uses_frontface
= TRUE
;
230 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
231 unsigned index
= fulldecl
->Range
.First
;
233 info
->system_value_semantic_name
[index
] = semName
;
234 info
->num_system_values
= MAX2(info
->num_system_values
,
237 if (semName
== TGSI_SEMANTIC_INSTANCEID
) {
238 info
->uses_instanceid
= TRUE
;
240 else if (semName
== TGSI_SEMANTIC_VERTEXID
) {
241 info
->uses_vertexid
= TRUE
;
243 else if (semName
== TGSI_SEMANTIC_VERTEXID_NOBASE
) {
244 info
->uses_vertexid_nobase
= TRUE
;
246 else if (semName
== TGSI_SEMANTIC_BASEVERTEX
) {
247 info
->uses_basevertex
= TRUE
;
249 else if (semName
== TGSI_SEMANTIC_PRIMID
) {
250 info
->uses_primid
= TRUE
;
253 else if (file
== TGSI_FILE_OUTPUT
) {
254 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
255 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
258 if (procType
== TGSI_PROCESSOR_VERTEX
||
259 procType
== TGSI_PROCESSOR_GEOMETRY
||
260 procType
== TGSI_PROCESSOR_TESS_CTRL
||
261 procType
== TGSI_PROCESSOR_TESS_EVAL
) {
262 if (semName
== TGSI_SEMANTIC_CLIPDIST
) {
263 info
->num_written_clipdistance
+=
264 util_bitcount(fulldecl
->Declaration
.UsageMask
);
265 info
->clipdist_writemask
|=
266 fulldecl
->Declaration
.UsageMask
<< (semIndex
*4);
268 else if (semName
== TGSI_SEMANTIC_CULLDIST
) {
269 info
->num_written_culldistance
+=
270 util_bitcount(fulldecl
->Declaration
.UsageMask
);
271 info
->culldist_writemask
|=
272 fulldecl
->Declaration
.UsageMask
<< (semIndex
*4);
274 else if (semName
== TGSI_SEMANTIC_VIEWPORT_INDEX
) {
275 info
->writes_viewport_index
= TRUE
;
277 else if (semName
== TGSI_SEMANTIC_LAYER
) {
278 info
->writes_layer
= TRUE
;
280 else if (semName
== TGSI_SEMANTIC_PSIZE
) {
281 info
->writes_psize
= TRUE
;
283 else if (semName
== TGSI_SEMANTIC_CLIPVERTEX
) {
284 info
->writes_clipvertex
= TRUE
;
288 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
289 if (semName
== TGSI_SEMANTIC_POSITION
) {
290 info
->writes_z
= TRUE
;
292 else if (semName
== TGSI_SEMANTIC_STENCIL
) {
293 info
->writes_stencil
= TRUE
;
297 if (procType
== TGSI_PROCESSOR_VERTEX
) {
298 if (semName
== TGSI_SEMANTIC_EDGEFLAG
) {
299 info
->writes_edgeflag
= TRUE
;
307 case TGSI_TOKEN_TYPE_IMMEDIATE
:
309 uint reg
= info
->immediate_count
++;
310 uint file
= TGSI_FILE_IMMEDIATE
;
312 info
->file_mask
[file
] |= (1 << reg
);
313 info
->file_count
[file
]++;
314 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
318 case TGSI_TOKEN_TYPE_PROPERTY
:
320 const struct tgsi_full_property
*fullprop
321 = &parse
.FullToken
.FullProperty
;
322 unsigned name
= fullprop
->Property
.PropertyName
;
324 assert(name
< Elements(info
->properties
));
325 info
->properties
[name
] = fullprop
->u
[0].Data
;
334 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
335 info
->opcode_count
[TGSI_OPCODE_KILL
]);
337 /* The dimensions of the IN decleration in geometry shader have
338 * to be deduced from the type of the input primitive.
340 if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
341 unsigned input_primitive
=
342 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
343 int num_verts
= u_vertices_per_prim(input_primitive
);
345 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
346 info
->file_max
[TGSI_FILE_INPUT
] =
347 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
348 for (j
= 0; j
< num_verts
; ++j
) {
349 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
353 tgsi_parse_free (&parse
);
359 * Check if the given shader is a "passthrough" shader consisting of only
360 * MOV instructions of the form: MOV OUT[n], IN[n]
364 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
366 struct tgsi_parse_context parse
;
369 ** Setup to begin parsing input shader
371 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
372 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
377 ** Loop over incoming program tokens/instructions
379 while (!tgsi_parse_end_of_tokens(&parse
)) {
381 tgsi_parse_token(&parse
);
383 switch (parse
.FullToken
.Token
.Type
) {
384 case TGSI_TOKEN_TYPE_INSTRUCTION
:
386 struct tgsi_full_instruction
*fullinst
=
387 &parse
.FullToken
.FullInstruction
;
388 const struct tgsi_full_src_register
*src
=
390 const struct tgsi_full_dst_register
*dst
=
393 /* Do a whole bunch of checks for a simple move */
394 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
395 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
396 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
397 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
398 src
->Register
.Index
!= dst
->Register
.Index
||
400 src
->Register
.Negate
||
401 src
->Register
.Absolute
||
403 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
404 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
405 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
406 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
408 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
410 tgsi_parse_free(&parse
);
416 case TGSI_TOKEN_TYPE_DECLARATION
:
418 case TGSI_TOKEN_TYPE_IMMEDIATE
:
420 case TGSI_TOKEN_TYPE_PROPERTY
:
427 tgsi_parse_free(&parse
);
429 /* if we get here, it's a pass-through shader */