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"
48 scan_instruction(struct tgsi_shader_info
*info
,
49 const struct tgsi_full_instruction
*fullinst
,
50 unsigned *current_depth
)
54 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
55 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
57 switch (fullinst
->Instruction
.Opcode
) {
60 case TGSI_OPCODE_BGNLOOP
:
62 info
->max_depth
= MAX2(info
->max_depth
, *current_depth
);
64 case TGSI_OPCODE_ENDIF
:
65 case TGSI_OPCODE_ENDLOOP
:
72 if (fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_CENTROID
||
73 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_OFFSET
||
74 fullinst
->Instruction
.Opcode
== TGSI_OPCODE_INTERP_SAMPLE
) {
75 const struct tgsi_full_src_register
*src0
= &fullinst
->Src
[0];
78 if (src0
->Register
.Indirect
&& src0
->Indirect
.ArrayID
)
79 input
= info
->input_array_first
[src0
->Indirect
.ArrayID
];
81 input
= src0
->Register
.Index
;
83 /* For the INTERP opcodes, the interpolation is always
84 * PERSPECTIVE unless LINEAR is specified.
86 switch (info
->input_interpolate
[input
]) {
87 case TGSI_INTERPOLATE_COLOR
:
88 case TGSI_INTERPOLATE_CONSTANT
:
89 case TGSI_INTERPOLATE_PERSPECTIVE
:
90 switch (fullinst
->Instruction
.Opcode
) {
91 case TGSI_OPCODE_INTERP_CENTROID
:
92 info
->uses_persp_opcode_interp_centroid
= TRUE
;
94 case TGSI_OPCODE_INTERP_OFFSET
:
95 info
->uses_persp_opcode_interp_offset
= TRUE
;
97 case TGSI_OPCODE_INTERP_SAMPLE
:
98 info
->uses_persp_opcode_interp_sample
= TRUE
;
103 case TGSI_INTERPOLATE_LINEAR
:
104 switch (fullinst
->Instruction
.Opcode
) {
105 case TGSI_OPCODE_INTERP_CENTROID
:
106 info
->uses_linear_opcode_interp_centroid
= TRUE
;
108 case TGSI_OPCODE_INTERP_OFFSET
:
109 info
->uses_linear_opcode_interp_offset
= TRUE
;
111 case TGSI_OPCODE_INTERP_SAMPLE
:
112 info
->uses_linear_opcode_interp_sample
= TRUE
;
119 if (fullinst
->Instruction
.Opcode
>= TGSI_OPCODE_F2D
&&
120 fullinst
->Instruction
.Opcode
<= TGSI_OPCODE_DSSG
)
121 info
->uses_doubles
= TRUE
;
123 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
124 const struct tgsi_full_src_register
*src
= &fullinst
->Src
[i
];
125 int ind
= src
->Register
.Index
;
127 /* Mark which inputs are effectively used */
128 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
130 usage_mask
= tgsi_util_get_inst_usage_mask(fullinst
, i
);
131 if (src
->Register
.Indirect
) {
132 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
133 info
->input_usage_mask
[ind
] |= usage_mask
;
137 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
138 info
->input_usage_mask
[ind
] |= usage_mask
;
141 if (info
->processor
== TGSI_PROCESSOR_FRAGMENT
&&
142 !src
->Register
.Indirect
) {
144 info
->input_semantic_name
[src
->Register
.Index
];
146 info
->input_semantic_index
[src
->Register
.Index
];
148 if (name
== TGSI_SEMANTIC_POSITION
&&
149 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
150 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
151 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
152 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
))
153 info
->reads_z
= TRUE
;
155 if (name
== TGSI_SEMANTIC_COLOR
) {
157 (1 << src
->Register
.SwizzleX
) |
158 (1 << src
->Register
.SwizzleY
) |
159 (1 << src
->Register
.SwizzleZ
) |
160 (1 << src
->Register
.SwizzleW
);
162 info
->colors_read
|= mask
<< (index
* 4);
167 /* check for indirect register reads */
168 if (src
->Register
.Indirect
) {
169 info
->indirect_files
|= (1 << src
->Register
.File
);
170 info
->indirect_files_read
|= (1 << src
->Register
.File
);
174 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
175 assert(fullinst
->Instruction
.Texture
);
176 assert(src
->Register
.Index
< Elements(info
->is_msaa_sampler
));
178 if (fullinst
->Instruction
.Texture
&&
179 (fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_MSAA
||
180 fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_ARRAY_MSAA
)) {
181 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
186 /* check for indirect register writes */
187 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
188 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
189 if (dst
->Register
.Indirect
) {
190 info
->indirect_files
|= (1 << dst
->Register
.File
);
191 info
->indirect_files_written
|= (1 << dst
->Register
.File
);
195 info
->num_instructions
++;
200 scan_declaration(struct tgsi_shader_info
*info
,
201 const struct tgsi_full_declaration
*fulldecl
)
203 const uint file
= fulldecl
->Declaration
.File
;
204 const unsigned procType
= info
->processor
;
207 if (fulldecl
->Declaration
.Array
) {
208 unsigned array_id
= fulldecl
->Array
.ArrayID
;
211 case TGSI_FILE_INPUT
:
212 assert(array_id
< ARRAY_SIZE(info
->input_array_first
));
213 info
->input_array_first
[array_id
] = fulldecl
->Range
.First
;
214 info
->input_array_last
[array_id
] = fulldecl
->Range
.Last
;
216 case TGSI_FILE_OUTPUT
:
217 assert(array_id
< ARRAY_SIZE(info
->output_array_first
));
218 info
->output_array_first
[array_id
] = fulldecl
->Range
.First
;
219 info
->output_array_last
[array_id
] = fulldecl
->Range
.Last
;
222 info
->array_max
[file
] = MAX2(info
->array_max
[file
], array_id
);
225 for (reg
= fulldecl
->Range
.First
; reg
<= fulldecl
->Range
.Last
; reg
++) {
226 unsigned semName
= fulldecl
->Semantic
.Name
;
227 unsigned semIndex
= fulldecl
->Semantic
.Index
+
228 (reg
- fulldecl
->Range
.First
);
230 /* only first 32 regs will appear in this bitfield */
231 info
->file_mask
[file
] |= (1 << reg
);
232 info
->file_count
[file
]++;
233 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
235 if (file
== TGSI_FILE_CONSTANT
) {
238 if (fulldecl
->Declaration
.Dimension
)
239 buffer
= fulldecl
->Dim
.Index2D
;
241 info
->const_file_max
[buffer
] =
242 MAX2(info
->const_file_max
[buffer
], (int)reg
);
244 else if (file
== TGSI_FILE_INPUT
) {
245 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
246 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
247 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
248 info
->input_interpolate_loc
[reg
] = (ubyte
)fulldecl
->Interp
.Location
;
249 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
252 /* Only interpolated varyings. Don't include POSITION.
253 * Don't include integer varyings, because they are not
256 if (semName
== TGSI_SEMANTIC_GENERIC
||
257 semName
== TGSI_SEMANTIC_TEXCOORD
||
258 semName
== TGSI_SEMANTIC_COLOR
||
259 semName
== TGSI_SEMANTIC_BCOLOR
||
260 semName
== TGSI_SEMANTIC_FOG
||
261 semName
== TGSI_SEMANTIC_CLIPDIST
||
262 semName
== TGSI_SEMANTIC_CULLDIST
) {
263 switch (fulldecl
->Interp
.Interpolate
) {
264 case TGSI_INTERPOLATE_COLOR
:
265 case TGSI_INTERPOLATE_PERSPECTIVE
:
266 switch (fulldecl
->Interp
.Location
) {
267 case TGSI_INTERPOLATE_LOC_CENTER
:
268 info
->uses_persp_center
= TRUE
;
270 case TGSI_INTERPOLATE_LOC_CENTROID
:
271 info
->uses_persp_centroid
= TRUE
;
273 case TGSI_INTERPOLATE_LOC_SAMPLE
:
274 info
->uses_persp_sample
= TRUE
;
278 case TGSI_INTERPOLATE_LINEAR
:
279 switch (fulldecl
->Interp
.Location
) {
280 case TGSI_INTERPOLATE_LOC_CENTER
:
281 info
->uses_linear_center
= TRUE
;
283 case TGSI_INTERPOLATE_LOC_CENTROID
:
284 info
->uses_linear_centroid
= TRUE
;
286 case TGSI_INTERPOLATE_LOC_SAMPLE
:
287 info
->uses_linear_sample
= TRUE
;
291 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
295 if (semName
== TGSI_SEMANTIC_PRIMID
)
296 info
->uses_primid
= TRUE
;
297 else if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
298 if (semName
== TGSI_SEMANTIC_POSITION
)
299 info
->reads_position
= TRUE
;
300 else if (semName
== TGSI_SEMANTIC_FACE
)
301 info
->uses_frontface
= TRUE
;
304 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
305 unsigned index
= fulldecl
->Range
.First
;
307 info
->system_value_semantic_name
[index
] = semName
;
308 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
311 case TGSI_SEMANTIC_INSTANCEID
:
312 info
->uses_instanceid
= TRUE
;
314 case TGSI_SEMANTIC_VERTEXID
:
315 info
->uses_vertexid
= TRUE
;
317 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
318 info
->uses_vertexid_nobase
= TRUE
;
320 case TGSI_SEMANTIC_BASEVERTEX
:
321 info
->uses_basevertex
= TRUE
;
323 case TGSI_SEMANTIC_PRIMID
:
324 info
->uses_primid
= TRUE
;
326 case TGSI_SEMANTIC_INVOCATIONID
:
327 info
->uses_invocationid
= TRUE
;
329 case TGSI_SEMANTIC_POSITION
:
330 info
->reads_position
= TRUE
;
332 case TGSI_SEMANTIC_FACE
:
333 info
->uses_frontface
= TRUE
;
335 case TGSI_SEMANTIC_SAMPLEMASK
:
336 info
->reads_samplemask
= TRUE
;
340 else if (file
== TGSI_FILE_OUTPUT
) {
341 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
342 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
345 if (semName
== TGSI_SEMANTIC_COLOR
)
346 info
->colors_written
|= 1 << semIndex
;
348 if (procType
== TGSI_PROCESSOR_VERTEX
||
349 procType
== TGSI_PROCESSOR_GEOMETRY
||
350 procType
== TGSI_PROCESSOR_TESS_CTRL
||
351 procType
== TGSI_PROCESSOR_TESS_EVAL
) {
353 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
354 info
->writes_viewport_index
= TRUE
;
356 case TGSI_SEMANTIC_LAYER
:
357 info
->writes_layer
= TRUE
;
359 case TGSI_SEMANTIC_PSIZE
:
360 info
->writes_psize
= TRUE
;
362 case TGSI_SEMANTIC_CLIPVERTEX
:
363 info
->writes_clipvertex
= TRUE
;
368 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
370 case TGSI_SEMANTIC_POSITION
:
371 info
->writes_z
= TRUE
;
373 case TGSI_SEMANTIC_STENCIL
:
374 info
->writes_stencil
= TRUE
;
376 case TGSI_SEMANTIC_SAMPLEMASK
:
377 info
->writes_samplemask
= TRUE
;
382 if (procType
== TGSI_PROCESSOR_VERTEX
) {
383 if (semName
== TGSI_SEMANTIC_EDGEFLAG
) {
384 info
->writes_edgeflag
= TRUE
;
387 } else if (file
== TGSI_FILE_SAMPLER
) {
388 info
->samplers_declared
|= 1 << reg
;
395 scan_immediate(struct tgsi_shader_info
*info
)
397 uint reg
= info
->immediate_count
++;
398 uint file
= TGSI_FILE_IMMEDIATE
;
400 info
->file_mask
[file
] |= (1 << reg
);
401 info
->file_count
[file
]++;
402 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
407 scan_property(struct tgsi_shader_info
*info
,
408 const struct tgsi_full_property
*fullprop
)
410 unsigned name
= fullprop
->Property
.PropertyName
;
411 unsigned value
= fullprop
->u
[0].Data
;
413 assert(name
< Elements(info
->properties
));
414 info
->properties
[name
] = value
;
417 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
418 info
->num_written_clipdistance
= value
;
419 info
->clipdist_writemask
|= (1 << value
) - 1;
421 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
422 info
->num_written_culldistance
= value
;
423 info
->culldist_writemask
|= (1 << value
) - 1;
430 * Scan the given TGSI shader to collect information such as number of
431 * registers used, special instructions used, etc.
432 * \return info the result of the scan
435 tgsi_scan_shader(const struct tgsi_token
*tokens
,
436 struct tgsi_shader_info
*info
)
439 struct tgsi_parse_context parse
;
440 unsigned current_depth
= 0;
442 memset(info
, 0, sizeof(*info
));
443 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
444 info
->file_max
[i
] = -1;
445 for (i
= 0; i
< Elements(info
->const_file_max
); i
++)
446 info
->const_file_max
[i
] = -1;
447 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
450 ** Setup to begin parsing input shader
452 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
453 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
456 procType
= parse
.FullHeader
.Processor
.Processor
;
457 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
458 procType
== TGSI_PROCESSOR_VERTEX
||
459 procType
== TGSI_PROCESSOR_GEOMETRY
||
460 procType
== TGSI_PROCESSOR_TESS_CTRL
||
461 procType
== TGSI_PROCESSOR_TESS_EVAL
||
462 procType
== TGSI_PROCESSOR_COMPUTE
);
463 info
->processor
= procType
;
467 ** Loop over incoming program tokens/instructions
469 while( !tgsi_parse_end_of_tokens( &parse
) ) {
473 tgsi_parse_token( &parse
);
475 switch( parse
.FullToken
.Token
.Type
) {
476 case TGSI_TOKEN_TYPE_INSTRUCTION
:
477 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
480 case TGSI_TOKEN_TYPE_DECLARATION
:
481 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
483 case TGSI_TOKEN_TYPE_IMMEDIATE
:
484 scan_immediate(info
);
486 case TGSI_TOKEN_TYPE_PROPERTY
:
487 scan_property(info
, &parse
.FullToken
.FullProperty
);
490 assert(!"Unexpected TGSI token type");
494 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
495 info
->opcode_count
[TGSI_OPCODE_KILL
]);
497 /* The dimensions of the IN decleration in geometry shader have
498 * to be deduced from the type of the input primitive.
500 if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
501 unsigned input_primitive
=
502 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
503 int num_verts
= u_vertices_per_prim(input_primitive
);
505 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
506 info
->file_max
[TGSI_FILE_INPUT
] =
507 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
508 for (j
= 0; j
< num_verts
; ++j
) {
509 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
513 tgsi_parse_free (&parse
);
519 * Check if the given shader is a "passthrough" shader consisting of only
520 * MOV instructions of the form: MOV OUT[n], IN[n]
524 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
526 struct tgsi_parse_context parse
;
529 ** Setup to begin parsing input shader
531 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
532 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
537 ** Loop over incoming program tokens/instructions
539 while (!tgsi_parse_end_of_tokens(&parse
)) {
541 tgsi_parse_token(&parse
);
543 switch (parse
.FullToken
.Token
.Type
) {
544 case TGSI_TOKEN_TYPE_INSTRUCTION
:
546 struct tgsi_full_instruction
*fullinst
=
547 &parse
.FullToken
.FullInstruction
;
548 const struct tgsi_full_src_register
*src
=
550 const struct tgsi_full_dst_register
*dst
=
553 /* Do a whole bunch of checks for a simple move */
554 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
555 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
556 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
557 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
558 src
->Register
.Index
!= dst
->Register
.Index
||
560 src
->Register
.Negate
||
561 src
->Register
.Absolute
||
563 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
564 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
565 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
566 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
568 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
570 tgsi_parse_free(&parse
);
576 case TGSI_TOKEN_TYPE_DECLARATION
:
578 case TGSI_TOKEN_TYPE_IMMEDIATE
:
580 case TGSI_TOKEN_TYPE_PROPERTY
:
587 tgsi_parse_free(&parse
);
589 /* if we get here, it's a pass-through shader */