1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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;
65 ** Setup to begin parsing input shader
67 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
68 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
71 procType
= parse
.FullHeader
.Processor
.Processor
;
72 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
73 procType
== TGSI_PROCESSOR_VERTEX
||
74 procType
== TGSI_PROCESSOR_GEOMETRY
||
75 procType
== TGSI_PROCESSOR_COMPUTE
);
76 info
->processor
= procType
;
80 ** Loop over incoming program tokens/instructions
82 while( !tgsi_parse_end_of_tokens( &parse
) ) {
86 tgsi_parse_token( &parse
);
88 switch( parse
.FullToken
.Token
.Type
) {
89 case TGSI_TOKEN_TYPE_INSTRUCTION
:
91 const struct tgsi_full_instruction
*fullinst
92 = &parse
.FullToken
.FullInstruction
;
95 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
96 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
98 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
99 const struct tgsi_full_src_register
*src
=
101 int ind
= src
->Register
.Index
;
103 /* Mark which inputs are effectively used */
104 if (src
->Register
.File
== TGSI_FILE_INPUT
) {
106 usage_mask
= tgsi_util_get_inst_usage_mask(fullinst
, i
);
107 if (src
->Register
.Indirect
) {
108 for (ind
= 0; ind
< info
->num_inputs
; ++ind
) {
109 info
->input_usage_mask
[ind
] |= usage_mask
;
113 assert(ind
< PIPE_MAX_SHADER_INPUTS
);
114 info
->input_usage_mask
[ind
] |= usage_mask
;
117 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
118 info
->reads_position
&&
119 src
->Register
.Index
== 0 &&
120 (src
->Register
.SwizzleX
== TGSI_SWIZZLE_Z
||
121 src
->Register
.SwizzleY
== TGSI_SWIZZLE_Z
||
122 src
->Register
.SwizzleZ
== TGSI_SWIZZLE_Z
||
123 src
->Register
.SwizzleW
== TGSI_SWIZZLE_Z
)) {
124 info
->reads_z
= TRUE
;
128 /* check for indirect register reads */
129 if (src
->Register
.Indirect
) {
130 info
->indirect_files
|= (1 << src
->Register
.File
);
134 if (src
->Register
.File
== TGSI_FILE_SAMPLER
) {
135 assert(fullinst
->Instruction
.Texture
);
136 assert(src
->Register
.Index
< Elements(info
->is_msaa_sampler
));
138 if (fullinst
->Instruction
.Texture
&&
139 (fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_MSAA
||
140 fullinst
->Texture
.Texture
== TGSI_TEXTURE_2D_ARRAY_MSAA
)) {
141 info
->is_msaa_sampler
[src
->Register
.Index
] = TRUE
;
146 /* check for indirect register writes */
147 for (i
= 0; i
< fullinst
->Instruction
.NumDstRegs
; i
++) {
148 const struct tgsi_full_dst_register
*dst
= &fullinst
->Dst
[i
];
149 if (dst
->Register
.Indirect
) {
150 info
->indirect_files
|= (1 << dst
->Register
.File
);
154 info
->num_instructions
++;
158 case TGSI_TOKEN_TYPE_DECLARATION
:
160 const struct tgsi_full_declaration
*fulldecl
161 = &parse
.FullToken
.FullDeclaration
;
162 const uint file
= fulldecl
->Declaration
.File
;
164 for (reg
= fulldecl
->Range
.First
;
165 reg
<= fulldecl
->Range
.Last
;
167 unsigned semName
= fulldecl
->Semantic
.Name
;
168 unsigned semIndex
= fulldecl
->Semantic
.Index
;
170 /* only first 32 regs will appear in this bitfield */
171 info
->file_mask
[file
] |= (1 << reg
);
172 info
->file_count
[file
]++;
173 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
175 if (file
== TGSI_FILE_INPUT
) {
176 info
->input_semantic_name
[reg
] = (ubyte
) semName
;
177 info
->input_semantic_index
[reg
] = (ubyte
) semIndex
;
178 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Interp
.Interpolate
;
179 info
->input_centroid
[reg
] = (ubyte
)fulldecl
->Interp
.Centroid
;
180 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Interp
.CylindricalWrap
;
183 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
184 if (semName
== TGSI_SEMANTIC_POSITION
)
185 info
->reads_position
= TRUE
;
186 else if (semName
== TGSI_SEMANTIC_PRIMID
)
187 info
->uses_primid
= TRUE
;
188 else if (semName
== TGSI_SEMANTIC_FACE
)
189 info
->uses_frontface
= TRUE
;
192 else if (file
== TGSI_FILE_SYSTEM_VALUE
) {
193 unsigned index
= fulldecl
->Range
.First
;
195 info
->system_value_semantic_name
[index
] = semName
;
196 info
->num_system_values
= MAX2(info
->num_system_values
,
199 if (semName
== TGSI_SEMANTIC_INSTANCEID
) {
200 info
->uses_instanceid
= TRUE
;
202 else if (semName
== TGSI_SEMANTIC_VERTEXID
) {
203 info
->uses_vertexid
= TRUE
;
205 else if (semName
== TGSI_SEMANTIC_PRIMID
) {
206 info
->uses_primid
= TRUE
;
209 else if (file
== TGSI_FILE_OUTPUT
) {
210 info
->output_semantic_name
[reg
] = (ubyte
) semName
;
211 info
->output_semantic_index
[reg
] = (ubyte
) semIndex
;
214 if (procType
== TGSI_PROCESSOR_VERTEX
||
215 procType
== TGSI_PROCESSOR_GEOMETRY
) {
216 if (semName
== TGSI_SEMANTIC_CLIPDIST
) {
217 info
->num_written_clipdistance
+=
218 util_bitcount(fulldecl
->Declaration
.UsageMask
);
220 else if (semName
== TGSI_SEMANTIC_CULLDIST
) {
221 info
->num_written_culldistance
+=
222 util_bitcount(fulldecl
->Declaration
.UsageMask
);
226 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
227 if (semName
== TGSI_SEMANTIC_POSITION
) {
228 info
->writes_z
= TRUE
;
230 else if (semName
== TGSI_SEMANTIC_STENCIL
) {
231 info
->writes_stencil
= TRUE
;
235 if (procType
== TGSI_PROCESSOR_VERTEX
) {
236 if (semName
== TGSI_SEMANTIC_EDGEFLAG
) {
237 info
->writes_edgeflag
= TRUE
;
241 if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
242 if (semName
== TGSI_SEMANTIC_VIEWPORT_INDEX
) {
243 info
->writes_viewport_index
= TRUE
;
245 else if (semName
== TGSI_SEMANTIC_LAYER
) {
246 info
->writes_layer
= TRUE
;
254 case TGSI_TOKEN_TYPE_IMMEDIATE
:
256 uint reg
= info
->immediate_count
++;
257 uint file
= TGSI_FILE_IMMEDIATE
;
259 info
->file_mask
[file
] |= (1 << reg
);
260 info
->file_count
[file
]++;
261 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
265 case TGSI_TOKEN_TYPE_PROPERTY
:
267 const struct tgsi_full_property
*fullprop
268 = &parse
.FullToken
.FullProperty
;
270 info
->properties
[info
->num_properties
].name
=
271 fullprop
->Property
.PropertyName
;
272 memcpy(info
->properties
[info
->num_properties
].data
,
273 fullprop
->u
, 8 * sizeof(unsigned));;
275 ++info
->num_properties
;
284 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KILL_IF
] ||
285 info
->opcode_count
[TGSI_OPCODE_KILL
]);
287 /* extract simple properties */
288 for (i
= 0; i
< info
->num_properties
; ++i
) {
289 switch (info
->properties
[i
].name
) {
290 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
291 info
->origin_lower_left
= info
->properties
[i
].data
[0];
293 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
294 info
->pixel_center_integer
= info
->properties
[i
].data
[0];
296 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
:
297 info
->color0_writes_all_cbufs
= info
->properties
[i
].data
[0];
299 case TGSI_PROPERTY_GS_INPUT_PRIM
:
300 /* The dimensions of the IN decleration in geometry shader have
301 * to be deduced from the type of the input primitive.
303 if (procType
== TGSI_PROCESSOR_GEOMETRY
) {
304 unsigned input_primitive
= info
->properties
[i
].data
[0];
305 int num_verts
= u_vertices_per_prim(input_primitive
);
307 info
->file_count
[TGSI_FILE_INPUT
] = num_verts
;
308 info
->file_max
[TGSI_FILE_INPUT
] =
309 MAX2(info
->file_max
[TGSI_FILE_INPUT
], num_verts
- 1);
310 for (j
= 0; j
< num_verts
; ++j
) {
311 info
->file_mask
[TGSI_FILE_INPUT
] |= (1 << j
);
320 tgsi_parse_free (&parse
);
326 * Check if the given shader is a "passthrough" shader consisting of only
327 * MOV instructions of the form: MOV OUT[n], IN[n]
331 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
333 struct tgsi_parse_context parse
;
336 ** Setup to begin parsing input shader
338 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
339 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
344 ** Loop over incoming program tokens/instructions
346 while (!tgsi_parse_end_of_tokens(&parse
)) {
348 tgsi_parse_token(&parse
);
350 switch (parse
.FullToken
.Token
.Type
) {
351 case TGSI_TOKEN_TYPE_INSTRUCTION
:
353 struct tgsi_full_instruction
*fullinst
=
354 &parse
.FullToken
.FullInstruction
;
355 const struct tgsi_full_src_register
*src
=
357 const struct tgsi_full_dst_register
*dst
=
360 /* Do a whole bunch of checks for a simple move */
361 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
362 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
363 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
364 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
365 src
->Register
.Index
!= dst
->Register
.Index
||
367 src
->Register
.Negate
||
368 src
->Register
.Absolute
||
370 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
371 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
372 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
373 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
375 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
377 tgsi_parse_free(&parse
);
383 case TGSI_TOKEN_TYPE_DECLARATION
:
385 case TGSI_TOKEN_TYPE_IMMEDIATE
:
387 case TGSI_TOKEN_TYPE_PROPERTY
:
394 tgsi_parse_free(&parse
);
396 /* if we get here, it's a pass-through shader */