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
;
59 unsigned current_depth
= 0;
61 memset(info
, 0, sizeof(*info
));
62 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
63 info
->file_max
[i
] = -1;
64 for (i
= 0; i
< Elements(info
->const_file_max
); i
++)
65 info
->const_file_max
[i
] = -1;
66 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
69 ** Setup to begin parsing input shader
71 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
72 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
75 procType
= parse
.FullHeader
.Processor
.Processor
;
76 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
77 procType
== TGSI_PROCESSOR_VERTEX
||
78 procType
== TGSI_PROCESSOR_GEOMETRY
||
79 procType
== TGSI_PROCESSOR_TESS_CTRL
||
80 procType
== TGSI_PROCESSOR_TESS_EVAL
||
81 procType
== TGSI_PROCESSOR_COMPUTE
);
82 info
->processor
= procType
;
86 ** Loop over incoming program tokens/instructions
88 while( !tgsi_parse_end_of_tokens( &parse
) ) {
92 tgsi_parse_token( &parse
);
94 switch( parse
.FullToken
.Token
.Type
) {
95 case TGSI_TOKEN_TYPE_INSTRUCTION
:
97 const struct tgsi_full_instruction
*fullinst
98 = &parse
.FullToken
.FullInstruction
;
101 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
102 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
104 switch (fullinst
->Instruction
.Opcode
) {
106 case TGSI_OPCODE_UIF
:
107 case TGSI_OPCODE_BGNLOOP
:
109 info
->max_depth
= MAX2(info
->max_depth
, current_depth
);
111 case TGSI_OPCODE_ENDIF
:
112 case TGSI_OPCODE_ENDLOOP
:
119 if (fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_CENTROID
||
120 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
121 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
122 const struct tgsi_full_src_register
*src0
= &fullinst
->Src
[0];
125 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
126 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
128 input
= src0
->Register
.Index
;
130 /* For the INTERP opcodes, the interpolation is always
131 * PERSPECTIVE unless LINEAR is specified.
133 switch (info
->input_interpolate
[input
]) {
134 case TGSI_INTERPOLATE_COLOR
:
135 case TGSI_INTERPOLATE_CONSTANT
:
136 case TGSI_INTERPOLATE_PERSPECTIVE
:
137 switch (fullinst
->Instruction
.Opcode
) {
138 case TGSI_OPCODE_INTERP_CENTROID
:
139 info
->uses_persp_opcode_interp_centroid
= true;
141 case TGSI_OPCODE_INTERP_OFFSET
:
142 info
->uses_persp_opcode_interp_offset
= true;
144 case TGSI_OPCODE_INTERP_SAMPLE
:
145 info
->uses_persp_opcode_interp_sample
= true;
150 case TGSI_INTERPOLATE_LINEAR
:
151 switch (fullinst
->Instruction
.Opcode
) {
152 case TGSI_OPCODE_INTERP_CENTROID
:
153 info
->uses_linear_opcode_interp_centroid
= true;
155 case TGSI_OPCODE_INTERP_OFFSET
:
156 info
->uses_linear_opcode_interp_offset
= true;
158 case TGSI_OPCODE_INTERP_SAMPLE
:
159 info
->uses_linear_opcode_interp_sample
= true;
166 if (fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
167 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
)
168 info
->uses_doubles
= true;
170 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
171 const struct tgsi_full_src_register
*src
=
173 int ind
= src
->Register
.Index
;
175 /* Mark which inputs are effectively used */
176 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
178 usage_mask
= tgsi_util_get_inst_usage_mask(fullinst
, i
);
179 if (src
->Register
.Indirect
) {
180 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
181 info
->input_usage_mask
[ind
] |= usage_mask
;
185 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
186 info
->input_usage_mask
[ind
] |= usage_mask
;
189 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
190 !src
->Register
.Indirect
) {
192 info
->input_semantic_name
[src
->Register
.Index
];
194 info
->input_semantic_index
[src
->Register
.Index
];
196 if (name
== TGSI_SEMANTIC_POSITION
&&
197 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
198 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
199 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
200 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
))
201 info
->reads_z
= TRUE
;
203 if (name
== TGSI_SEMANTIC_COLOR
) {
205 (1 << src
->Register
.SwizzleX
) |
206 (1 << src
->Register
.SwizzleY
) |
207 (1 << src
->Register
.SwizzleZ
) |
208 (1 << src
->Register
.SwizzleW
);
210 info
->colors_read
|= mask
<< (index
* 4);
215 /* check for indirect register reads */
216 if (src
->Register
.Indirect
) {
217 info
->indirect_files
|= (1 << src
->Register
.File
);
218 info
->indirect_files_read
|= (1 << src
->Register
.File
);
222 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
223 assert(fullinst
->Instruction
.Texture
);
224 assert(src
->Register
.Index
< Elements(info
->is_msaa_sampler
));
226 if (fullinst
->Instruction
.Texture
&&
227 (fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_MSAA
||
228 fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_ARRAY_MSAA
)) {
229 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
234 /* check for indirect register writes */
235 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
236 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
237 if (dst
->Register
.Indirect
) {
238 info
->indirect_files
|= (1 << dst
->Register
.File
);
239 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
243 info
->num_instructions
++;
247 case TGSI_TOKEN_TYPE_DECLARATION
:
249 const struct tgsi_full_declaration
*fulldecl
250 = &parse
.FullToken
.FullDeclaration
;
251 const uint file
= fulldecl
->Declaration
.File
;
254 if (fulldecl
->Declaration
.Array
) {
255 unsigned array_id
= fulldecl
->Array
.ArrayID
;
258 case TGSI_FILE_INPUT
:
259 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
260 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
261 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
263 case TGSI_FILE_OUTPUT
:
264 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
265 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
266 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
269 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
272 for (reg
= fulldecl
->Range
.First
;
273 reg
<= fulldecl
->Range
.Last
;
275 unsigned semName
= fulldecl
->Semantic
.Name
;
277 fulldecl
->Semantic
.Index
+ (reg
- fulldecl
->Range
.First
);
279 /* only first 32 regs will appear in this bitfield */
280 info
->file_mask
[file
] |= (1 << reg
);
281 info
->file_count
[file
]++;
282 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
284 if (file
== TGSI_FILE_CONSTANT
) {
287 if (fulldecl
->Declaration
.Dimension
)
288 buffer
= fulldecl
->Dim
.Index2D
;
290 info
->const_file_max
[buffer
] =
291 MAX2(info
->const_file_max
[buffer
], (int)reg
);
293 else if (file
== TGSI_FILE_INPUT
) {
294 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
295 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
296 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
297 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
298 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
301 /* Only interpolated varyings. Don't include POSITION.
302 * Don't include integer varyings, because they are not
305 if (semName
== TGSI_SEMANTIC_GENERIC
||
306 semName
== TGSI_SEMANTIC_TEXCOORD
||
307 semName
== TGSI_SEMANTIC_COLOR
||
308 semName
== TGSI_SEMANTIC_BCOLOR
||
309 semName
== TGSI_SEMANTIC_FOG
||
310 semName
== TGSI_SEMANTIC_CLIPDIST
||
311 semName
== TGSI_SEMANTIC_CULLDIST
) {
312 switch (fulldecl
->Interp
.Interpolate
) {
313 case TGSI_INTERPOLATE_COLOR
:
314 case TGSI_INTERPOLATE_PERSPECTIVE
:
315 switch (fulldecl
->Interp
.Location
) {
316 case TGSI_INTERPOLATE_LOC_CENTER
:
317 info
->uses_persp_center
= true;
319 case TGSI_INTERPOLATE_LOC_CENTROID
:
320 info
->uses_persp_centroid
= true;
322 case TGSI_INTERPOLATE_LOC_SAMPLE
:
323 info
->uses_persp_sample
= true;
327 case TGSI_INTERPOLATE_LINEAR
:
328 switch (fulldecl
->Interp
.Location
) {
329 case TGSI_INTERPOLATE_LOC_CENTER
:
330 info
->uses_linear_center
= true;
332 case TGSI_INTERPOLATE_LOC_CENTROID
:
333 info
->uses_linear_centroid
= true;
335 case TGSI_INTERPOLATE_LOC_SAMPLE
:
336 info
->uses_linear_sample
= true;
340 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
344 if (semName
== TGSI_SEMANTIC_PRIMID
)
345 info
->uses_primid
= TRUE
;
346 else if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
347 if (semName
== TGSI_SEMANTIC_POSITION
)
348 info
->reads_position
= TRUE
;
349 else if (semName
== TGSI_SEMANTIC_FACE
)
350 info
->uses_frontface
= TRUE
;
353 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
354 unsigned index
= fulldecl
->Range
.First
;
356 info
->system_value_semantic_name
[index
] = semName
;
357 info
->num_system_values
= MAX2(info
->num_system_values
,
360 if (semName
== TGSI_SEMANTIC_INSTANCEID
) {
361 info
->uses_instanceid
= TRUE
;
363 else if (semName
== TGSI_SEMANTIC_VERTEXID
) {
364 info
->uses_vertexid
= TRUE
;
366 else if (semName
== TGSI_SEMANTIC_VERTEXID_NOBASE
) {
367 info
->uses_vertexid_nobase
= TRUE
;
369 else if (semName
== TGSI_SEMANTIC_BASEVERTEX
) {
370 info
->uses_basevertex
= TRUE
;
372 else if (semName
== TGSI_SEMANTIC_PRIMID
) {
373 info
->uses_primid
= TRUE
;
374 } else if (semName
== TGSI_SEMANTIC_INVOCATIONID
) {
375 info
->uses_invocationid
= TRUE
;
378 else if (file
== TGSI_FILE_OUTPUT
) {
379 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
380 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
383 if (semName
== TGSI_SEMANTIC_COLOR
)
384 info
->colors_written
|= 1 << semIndex
;
386 if (procType
== TGSI_PROCESSOR_VERTEX
||
387 procType
== TGSI_PROCESSOR_GEOMETRY
||
388 procType
== TGSI_PROCESSOR_TESS_CTRL
||
389 procType
== TGSI_PROCESSOR_TESS_EVAL
) {
390 if (semName
== TGSI_SEMANTIC_VIEWPORT_INDEX
) {
391 info
->writes_viewport_index
= TRUE
;
393 else if (semName
== TGSI_SEMANTIC_LAYER
) {
394 info
->writes_layer
= TRUE
;
396 else if (semName
== TGSI_SEMANTIC_PSIZE
) {
397 info
->writes_psize
= TRUE
;
399 else if (semName
== TGSI_SEMANTIC_CLIPVERTEX
) {
400 info
->writes_clipvertex
= TRUE
;
404 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
405 if (semName
== TGSI_SEMANTIC_POSITION
) {
406 info
->writes_z
= TRUE
;
408 else if (semName
== TGSI_SEMANTIC_STENCIL
) {
409 info
->writes_stencil
= TRUE
;
410 } else if (semName
== TGSI_SEMANTIC_SAMPLEMASK
) {
411 info
->writes_samplemask
= TRUE
;
415 if (procType
== TGSI_PROCESSOR_VERTEX
) {
416 if (semName
== TGSI_SEMANTIC_EDGEFLAG
) {
417 info
->writes_edgeflag
= TRUE
;
420 } else if (file
== TGSI_FILE_SAMPLER
) {
421 info
->samplers_declared
|= 1 << reg
;
427 case TGSI_TOKEN_TYPE_IMMEDIATE
:
429 uint reg
= info
->immediate_count
++;
430 uint file
= TGSI_FILE_IMMEDIATE
;
432 info
->file_mask
[file
] |= (1 << reg
);
433 info
->file_count
[file
]++;
434 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
438 case TGSI_TOKEN_TYPE_PROPERTY
:
440 const struct tgsi_full_property
*fullprop
441 = &parse
.FullToken
.FullProperty
;
442 unsigned name
= fullprop
->Property
.PropertyName
;
443 unsigned value
= fullprop
->u
[0].Data
;
445 assert(name
< Elements(info
->properties
));
446 info
->properties
[name
] = value
;
449 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
450 info
->num_written_clipdistance
= value
;
451 info
->clipdist_writemask
|= (1 << value
) - 1;
453 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
454 info
->num_written_culldistance
= value
;
455 info
->culldist_writemask
|= (1 << value
) - 1;
466 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
467 info
->opcode_count
[TGSI_OPCODE_KILL
]);
469 /* The dimensions of the IN decleration in geometry shader have
470 * to be deduced from the type of the input primitive.
472 if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
473 unsigned input_primitive
=
474 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
475 int num_verts
= u_vertices_per_prim(input_primitive
);
477 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
478 info
->file_max
[TGSI_FILE_INPUT
] =
479 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
480 for (j
= 0; j
< num_verts
; ++j
) {
481 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
485 tgsi_parse_free (&parse
);
491 * Check if the given shader is a "passthrough" shader consisting of only
492 * MOV instructions of the form: MOV OUT[n], IN[n]
496 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
498 struct tgsi_parse_context parse
;
501 ** Setup to begin parsing input shader
503 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
504 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
509 ** Loop over incoming program tokens/instructions
511 while (!tgsi_parse_end_of_tokens(&parse
)) {
513 tgsi_parse_token(&parse
);
515 switch (parse
.FullToken
.Token
.Type
) {
516 case TGSI_TOKEN_TYPE_INSTRUCTION
:
518 struct tgsi_full_instruction
*fullinst
=
519 &parse
.FullToken
.FullInstruction
;
520 const struct tgsi_full_src_register
*src
=
522 const struct tgsi_full_dst_register
*dst
=
525 /* Do a whole bunch of checks for a simple move */
526 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
527 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
528 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
529 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
530 src
->Register
.Index
!= dst
->Register
.Index
||
532 src
->Register
.Negate
||
533 src
->Register
.Absolute
||
535 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
536 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
537 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
538 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
540 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
542 tgsi_parse_free(&parse
);
548 case TGSI_TOKEN_TYPE_DECLARATION
:
550 case TGSI_TOKEN_TYPE_IMMEDIATE
:
552 case TGSI_TOKEN_TYPE_PROPERTY
:
559 tgsi_parse_free(&parse
);
561 /* if we get here, it's a pass-through shader */