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_math.h"
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_scan.h"
45 * Scan the given TGSI shader to collect information such as number of
46 * registers used, special instructions used, etc.
47 * \return info the result of the scan
50 tgsi_scan_shader(const struct tgsi_token
*tokens
,
51 struct tgsi_shader_info
*info
)
54 struct tgsi_parse_context parse
;
56 memset(info
, 0, sizeof(*info
));
57 for (i
= 0; i
< TGSI_FILE_COUNT
; i
++)
58 info
->file_max
[i
] = -1;
61 ** Setup to begin parsing input shader
63 if (tgsi_parse_init( &parse
, tokens
) != TGSI_PARSE_OK
) {
64 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
67 procType
= parse
.FullHeader
.Processor
.Processor
;
68 assert(procType
== TGSI_PROCESSOR_FRAGMENT
||
69 procType
== TGSI_PROCESSOR_VERTEX
||
70 procType
== TGSI_PROCESSOR_GEOMETRY
);
74 ** Loop over incoming program tokens/instructions
76 while( !tgsi_parse_end_of_tokens( &parse
) ) {
80 tgsi_parse_token( &parse
);
82 switch( parse
.FullToken
.Token
.Type
) {
83 case TGSI_TOKEN_TYPE_INSTRUCTION
:
85 const struct tgsi_full_instruction
*fullinst
86 = &parse
.FullToken
.FullInstruction
;
88 assert(fullinst
->Instruction
.Opcode
< TGSI_OPCODE_LAST
);
89 info
->opcode_count
[fullinst
->Instruction
.Opcode
]++;
91 /* special case: scan fragment shaders for use of the fog
92 * input/attribute. The X component is fog, the Y component
93 * is the front/back-face flag.
95 if (procType
== TGSI_PROCESSOR_FRAGMENT
) {
97 for (i
= 0; i
< fullinst
->Instruction
.NumSrcRegs
; i
++) {
98 const struct tgsi_full_src_register
*src
=
100 if (src
->Register
.File
== TGSI_FILE_INPUT
||
101 src
->Register
.File
== TGSI_FILE_SYSTEM_VALUE
) {
102 const int ind
= src
->Register
.Index
;
103 if (info
->input_semantic_name
[ind
] == TGSI_SEMANTIC_FOG
) {
104 info
->uses_fogcoord
= TRUE
;
106 else if (info
->input_semantic_name
[ind
] == TGSI_SEMANTIC_FACE
) {
107 info
->uses_frontfacing
= TRUE
;
115 case TGSI_TOKEN_TYPE_DECLARATION
:
117 const struct tgsi_full_declaration
*fulldecl
118 = &parse
.FullToken
.FullDeclaration
;
119 const uint file
= fulldecl
->Declaration
.File
;
121 for (reg
= fulldecl
->Range
.First
;
122 reg
<= fulldecl
->Range
.Last
;
125 /* only first 32 regs will appear in this bitfield */
126 info
->file_mask
[file
] |= (1 << reg
);
127 info
->file_count
[file
]++;
128 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
130 if (file
== TGSI_FILE_INPUT
|| file
== TGSI_FILE_SYSTEM_VALUE
) {
131 info
->input_semantic_name
[reg
] = (ubyte
)fulldecl
->Semantic
.Name
;
132 info
->input_semantic_index
[reg
] = (ubyte
)fulldecl
->Semantic
.Index
;
133 info
->input_interpolate
[reg
] = (ubyte
)fulldecl
->Declaration
.Interpolate
;
134 info
->input_cylindrical_wrap
[reg
] = (ubyte
)fulldecl
->Declaration
.CylindricalWrap
;
137 else if (file
== TGSI_FILE_OUTPUT
) {
138 info
->output_semantic_name
[reg
] = (ubyte
)fulldecl
->Semantic
.Name
;
139 info
->output_semantic_index
[reg
] = (ubyte
)fulldecl
->Semantic
.Index
;
142 /* extra info for special outputs */
143 if (procType
== TGSI_PROCESSOR_FRAGMENT
&&
144 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
145 info
->writes_z
= TRUE
;
147 if (procType
== TGSI_PROCESSOR_VERTEX
&&
148 fulldecl
->Semantic
.Name
== TGSI_SEMANTIC_EDGEFLAG
) {
149 info
->writes_edgeflag
= TRUE
;
157 case TGSI_TOKEN_TYPE_IMMEDIATE
:
159 uint reg
= info
->immediate_count
++;
160 uint file
= TGSI_FILE_IMMEDIATE
;
162 info
->file_mask
[file
] |= (1 << reg
);
163 info
->file_count
[file
]++;
164 info
->file_max
[file
] = MAX2(info
->file_max
[file
], (int)reg
);
167 case TGSI_TOKEN_TYPE_PROPERTY
:
169 const struct tgsi_full_property
*fullprop
170 = &parse
.FullToken
.FullProperty
;
172 info
->properties
[info
->num_properties
].name
=
173 fullprop
->Property
.PropertyName
;
174 memcpy(info
->properties
[info
->num_properties
].data
,
175 fullprop
->u
, 8 * sizeof(unsigned));;
177 ++info
->num_properties
;
186 info
->uses_kill
= (info
->opcode_count
[TGSI_OPCODE_KIL
] ||
187 info
->opcode_count
[TGSI_OPCODE_KILP
]);
189 tgsi_parse_free (&parse
);
195 * Check if the given shader is a "passthrough" shader consisting of only
196 * MOV instructions of the form: MOV OUT[n], IN[n]
200 tgsi_is_passthrough_shader(const struct tgsi_token
*tokens
)
202 struct tgsi_parse_context parse
;
205 ** Setup to begin parsing input shader
207 if (tgsi_parse_init(&parse
, tokens
) != TGSI_PARSE_OK
) {
208 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
213 ** Loop over incoming program tokens/instructions
215 while (!tgsi_parse_end_of_tokens(&parse
)) {
217 tgsi_parse_token(&parse
);
219 switch (parse
.FullToken
.Token
.Type
) {
220 case TGSI_TOKEN_TYPE_INSTRUCTION
:
222 struct tgsi_full_instruction
*fullinst
=
223 &parse
.FullToken
.FullInstruction
;
224 const struct tgsi_full_src_register
*src
=
226 const struct tgsi_full_dst_register
*dst
=
229 /* Do a whole bunch of checks for a simple move */
230 if (fullinst
->Instruction
.Opcode
!= TGSI_OPCODE_MOV
||
231 (src
->Register
.File
!= TGSI_FILE_INPUT
&&
232 src
->Register
.File
!= TGSI_FILE_SYSTEM_VALUE
) ||
233 dst
->Register
.File
!= TGSI_FILE_OUTPUT
||
234 src
->Register
.Index
!= dst
->Register
.Index
||
236 src
->Register
.Negate
||
237 src
->Register
.Absolute
||
239 src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
240 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
241 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
242 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
||
244 dst
->Register
.WriteMask
!= TGSI_WRITEMASK_XYZW
)
246 tgsi_parse_free(&parse
);
252 case TGSI_TOKEN_TYPE_DECLARATION
:
254 case TGSI_TOKEN_TYPE_IMMEDIATE
:
256 case TGSI_TOKEN_TYPE_PROPERTY
:
263 tgsi_parse_free(&parse
);
265 /* if we get here, it's a pass-through shader */