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
;
251 /* Vertex shaders can have inputs with holes between them. */
252 if (info
->processor
== TGSI_PROCESSOR_VERTEX
)
253 info
->num_inputs
= MAX2(info
->num_inputs
, reg
+ 1);
256 assert(reg
< info
->num_inputs
);
259 /* Only interpolated varyings. Don't include POSITION.
260 * Don't include integer varyings, because they are not
263 if (semName
== TGSI_SEMANTIC_GENERIC
||
264 semName
== TGSI_SEMANTIC_TEXCOORD
||
265 semName
== TGSI_SEMANTIC_COLOR
||
266 semName
== TGSI_SEMANTIC_BCOLOR
||
267 semName
== TGSI_SEMANTIC_FOG
||
268 semName
== TGSI_SEMANTIC_CLIPDIST
||
269 semName
== TGSI_SEMANTIC_CULLDIST
) {
270 switch (fulldecl
->Interp
.Interpolate
) {
271 case TGSI_INTERPOLATE_COLOR
:
272 case TGSI_INTERPOLATE_PERSPECTIVE
:
273 switch (fulldecl
->Interp
.Location
) {
274 case TGSI_INTERPOLATE_LOC_CENTER
:
275 info
->uses_persp_center
= TRUE
;
277 case TGSI_INTERPOLATE_LOC_CENTROID
:
278 info
->uses_persp_centroid
= TRUE
;
280 case TGSI_INTERPOLATE_LOC_SAMPLE
:
281 info
->uses_persp_sample
= TRUE
;
285 case TGSI_INTERPOLATE_LINEAR
:
286 switch (fulldecl
->Interp
.Location
) {
287 case TGSI_INTERPOLATE_LOC_CENTER
:
288 info
->uses_linear_center
= TRUE
;
290 case TGSI_INTERPOLATE_LOC_CENTROID
:
291 info
->uses_linear_centroid
= TRUE
;
293 case TGSI_INTERPOLATE_LOC_SAMPLE
:
294 info
->uses_linear_sample
= TRUE
;
298 /* TGSI_INTERPOLATE_CONSTANT doesn't do any interpolation. */
302 if (semName
== TGSI_SEMANTIC_PRIMID
)
303 info
->uses_primid
= TRUE
;
304 else if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
305 if (semName
== TGSI_SEMANTIC_POSITION
)
306 info
->reads_position
= TRUE
;
307 else if (semName
== TGSI_SEMANTIC_FACE
)
308 info
->uses_frontface
= TRUE
;
311 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
312 unsigned index
= fulldecl
->Range
.First
;
314 info
->system_value_semantic_name
[index
] = semName
;
315 info
->num_system_values
= MAX2(info
->num_system_values
, index
+ 1);
318 case TGSI_SEMANTIC_INSTANCEID
:
319 info
->uses_instanceid
= TRUE
;
321 case TGSI_SEMANTIC_VERTEXID
:
322 info
->uses_vertexid
= TRUE
;
324 case TGSI_SEMANTIC_VERTEXID_NOBASE
:
325 info
->uses_vertexid_nobase
= TRUE
;
327 case TGSI_SEMANTIC_BASEVERTEX
:
328 info
->uses_basevertex
= TRUE
;
330 case TGSI_SEMANTIC_PRIMID
:
331 info
->uses_primid
= TRUE
;
333 case TGSI_SEMANTIC_INVOCATIONID
:
334 info
->uses_invocationid
= TRUE
;
336 case TGSI_SEMANTIC_POSITION
:
337 info
->reads_position
= TRUE
;
339 case TGSI_SEMANTIC_FACE
:
340 info
->uses_frontface
= TRUE
;
342 case TGSI_SEMANTIC_SAMPLEMASK
:
343 info
->reads_samplemask
= TRUE
;
347 else if (file
== TGSI_FILE_OUTPUT
) {
348 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
349 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
351 assert(reg
< info
->num_outputs
);
353 if (semName
== TGSI_SEMANTIC_COLOR
)
354 info
->colors_written
|= 1 << semIndex
;
356 if (procType
== TGSI_PROCESSOR_VERTEX
||
357 procType
== TGSI_PROCESSOR_GEOMETRY
||
358 procType
== TGSI_PROCESSOR_TESS_CTRL
||
359 procType
== TGSI_PROCESSOR_TESS_EVAL
) {
361 case TGSI_SEMANTIC_VIEWPORT_INDEX
:
362 info
->writes_viewport_index
= TRUE
;
364 case TGSI_SEMANTIC_LAYER
:
365 info
->writes_layer
= TRUE
;
367 case TGSI_SEMANTIC_PSIZE
:
368 info
->writes_psize
= TRUE
;
370 case TGSI_SEMANTIC_CLIPVERTEX
:
371 info
->writes_clipvertex
= TRUE
;
376 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
378 case TGSI_SEMANTIC_POSITION
:
379 info
->writes_z
= TRUE
;
381 case TGSI_SEMANTIC_STENCIL
:
382 info
->writes_stencil
= TRUE
;
384 case TGSI_SEMANTIC_SAMPLEMASK
:
385 info
->writes_samplemask
= TRUE
;
390 if (procType
== TGSI_PROCESSOR_VERTEX
) {
391 if (semName
== TGSI_SEMANTIC_EDGEFLAG
) {
392 info
->writes_edgeflag
= TRUE
;
395 } else if (file
== TGSI_FILE_SAMPLER
) {
396 info
->samplers_declared
|= 1 << reg
;
403 scan_immediate(struct tgsi_shader_info
*info
)
405 uint reg
= info
->immediate_count
++;
406 uint file
= TGSI_FILE_IMMEDIATE
;
408 info
->file_mask
[file
] |= (1 << reg
);
409 info
->file_count
[file
]++;
410 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
415 scan_property(struct tgsi_shader_info
*info
,
416 const struct tgsi_full_property
*fullprop
)
418 unsigned name
= fullprop
->Property
.PropertyName
;
419 unsigned value
= fullprop
->u
[0].Data
;
421 assert(name
< Elements(info
->properties
));
422 info
->properties
[name
] = value
;
425 case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED
:
426 info
->num_written_clipdistance
= value
;
427 info
->clipdist_writemask
|= (1 << value
) - 1;
429 case TGSI_PROPERTY_NUM_CULLDIST_ENABLED
:
430 info
->num_written_culldistance
= value
;
431 info
->culldist_writemask
|= (1 << value
) - 1;
438 * Scan the given TGSI shader to collect information such as number of
439 * registers used, special instructions used, etc.
440 * \return info the result of the scan
443 tgsi_scan_shader(const struct tgsi_token
*tokens
,
444 struct tgsi_shader_info
*info
)
447 struct tgsi_parse_context parse
;
448 unsigned current_depth
= 0;
450 memset(info
, 0, sizeof(*info
));
451 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
452 info
->file_max
[i
] = -1;
453 for (i
= 0; i
< Elements(info
->const_file_max
); i
++)
454 info
->const_file_max
[i
] = -1;
455 info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
] = 1;
458 ** Setup to begin parsing input shader
460 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
461 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
464 procType
= parse
.FullHeader
.Processor
.Processor
;
465 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
466 procType
== TGSI_PROCESSOR_VERTEX
||
467 procType
== TGSI_PROCESSOR_GEOMETRY
||
468 procType
== TGSI_PROCESSOR_TESS_CTRL
||
469 procType
== TGSI_PROCESSOR_TESS_EVAL
||
470 procType
== TGSI_PROCESSOR_COMPUTE
);
471 info
->processor
= procType
;
474 ** Loop over incoming program tokens/instructions
476 while (!tgsi_parse_end_of_tokens(&parse
)) {
479 tgsi_parse_token( &parse
);
481 switch( parse
.FullToken
.Token
.Type
) {
482 case TGSI_TOKEN_TYPE_INSTRUCTION
:
483 scan_instruction(info
, &parse
.FullToken
.FullInstruction
,
486 case TGSI_TOKEN_TYPE_DECLARATION
:
487 scan_declaration(info
, &parse
.FullToken
.FullDeclaration
);
489 case TGSI_TOKEN_TYPE_IMMEDIATE
:
490 scan_immediate(info
);
492 case TGSI_TOKEN_TYPE_PROPERTY
:
493 scan_property(info
, &parse
.FullToken
.FullProperty
);
496 assert(!"Unexpected TGSI token type");
500 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
501 info
->opcode_count
[TGSI_OPCODE_KILL
]);
503 /* The dimensions of the IN decleration in geometry shader have
504 * to be deduced from the type of the input primitive.
506 if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
507 unsigned input_primitive
=
508 info
->properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
509 int num_verts
= u_vertices_per_prim(input_primitive
);
511 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
512 info
->file_max
[TGSI_FILE_INPUT
] =
513 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
514 for (j
= 0; j
< num_verts
; ++j
) {
515 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
519 tgsi_parse_free(&parse
);
525 * Check if the given shader is a "passthrough" shader consisting of only
526 * MOV instructions of the form: MOV OUT[n], IN[n]
530 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
532 struct tgsi_parse_context parse
;
535 ** Setup to begin parsing input shader
537 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
538 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
543 ** Loop over incoming program tokens/instructions
545 while (!tgsi_parse_end_of_tokens(&parse
)) {
547 tgsi_parse_token(&parse
);
549 switch (parse
.FullToken
.Token
.Type
) {
550 case TGSI_TOKEN_TYPE_INSTRUCTION
:
552 struct tgsi_full_instruction
*fullinst
=
553 &parse
.FullToken
.FullInstruction
;
554 const struct tgsi_full_src_register
*src
=
556 const struct tgsi_full_dst_register
*dst
=
559 /* Do a whole bunch of checks for a simple move */
560 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
561 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
562 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
563 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
564 src
->Register
.Index
!= dst
->Register
.Index
||
566 src
->Register
.Negate
||
567 src
->Register
.Absolute
||
569 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
570 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
571 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
572 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
574 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
576 tgsi_parse_free(&parse
);
582 case TGSI_TOKEN_TYPE_DECLARATION
:
584 case TGSI_TOKEN_TYPE_IMMEDIATE
:
586 case TGSI_TOKEN_TYPE_PROPERTY
:
593 tgsi_parse_free(&parse
);
595 /* if we get here, it's a pass-through shader */