tgsi: add scanner support for centroid inputs
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_scan.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2008 VMware, Inc. All rights Reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29 /**
30 * TGSI program scan utility.
31 * Used to determine which registers and instructions are used by a shader.
32 *
33 * Authors: Brian Paul
34 */
35
36
37 #include "util/u_math.h"
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_util.h"
40 #include "tgsi/tgsi_scan.h"
41
42
43
44
45 /**
46 * Scan the given TGSI shader to collect information such as number of
47 * registers used, special instructions used, etc.
48 * \return info the result of the scan
49 */
50 void
51 tgsi_scan_shader(const struct tgsi_token *tokens,
52 struct tgsi_shader_info *info)
53 {
54 uint procType, i;
55 struct tgsi_parse_context parse;
56
57 memset(info, 0, sizeof(*info));
58 for (i = 0; i < TGSI_FILE_COUNT; i++)
59 info->file_max[i] = -1;
60
61 /**
62 ** Setup to begin parsing input shader
63 **/
64 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
65 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
66 return;
67 }
68 procType = parse.FullHeader.Processor.Processor;
69 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
70 procType == TGSI_PROCESSOR_VERTEX ||
71 procType == TGSI_PROCESSOR_GEOMETRY);
72
73
74 /**
75 ** Loop over incoming program tokens/instructions
76 */
77 while( !tgsi_parse_end_of_tokens( &parse ) ) {
78
79 info->num_tokens++;
80
81 tgsi_parse_token( &parse );
82
83 switch( parse.FullToken.Token.Type ) {
84 case TGSI_TOKEN_TYPE_INSTRUCTION:
85 {
86 const struct tgsi_full_instruction *fullinst
87 = &parse.FullToken.FullInstruction;
88 uint i;
89
90 assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
91 info->opcode_count[fullinst->Instruction.Opcode]++;
92
93 for (i = 0; i < fullinst->Instruction.NumSrcRegs; i++) {
94 const struct tgsi_full_src_register *src =
95 &fullinst->Src[i];
96 int ind = src->Register.Index;
97
98 /* Mark which inputs are effectively used */
99 if (src->Register.File == TGSI_FILE_INPUT) {
100 unsigned usage_mask;
101 usage_mask = tgsi_util_get_inst_usage_mask(fullinst, i);
102 if (src->Register.Indirect) {
103 for (ind = 0; ind < info->num_inputs; ++ind) {
104 info->input_usage_mask[ind] |= usage_mask;
105 }
106 } else {
107 assert(ind >= 0);
108 assert(ind < PIPE_MAX_SHADER_INPUTS);
109 info->input_usage_mask[ind] |= usage_mask;
110 }
111 }
112
113 /* check for indirect register reads */
114 if (src->Register.Indirect) {
115 info->indirect_files |= (1 << src->Register.File);
116 }
117 }
118
119 /* check for indirect register writes */
120 for (i = 0; i < fullinst->Instruction.NumDstRegs; i++) {
121 const struct tgsi_full_dst_register *dst = &fullinst->Dst[i];
122 if (dst->Register.Indirect) {
123 info->indirect_files |= (1 << dst->Register.File);
124 }
125 }
126
127 info->num_instructions++;
128 }
129 break;
130
131 case TGSI_TOKEN_TYPE_DECLARATION:
132 {
133 const struct tgsi_full_declaration *fulldecl
134 = &parse.FullToken.FullDeclaration;
135 const uint file = fulldecl->Declaration.File;
136 uint reg;
137 for (reg = fulldecl->Range.First;
138 reg <= fulldecl->Range.Last;
139 reg++) {
140
141 /* only first 32 regs will appear in this bitfield */
142 info->file_mask[file] |= (1 << reg);
143 info->file_count[file]++;
144 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
145
146 if (file == TGSI_FILE_INPUT || file == TGSI_FILE_SYSTEM_VALUE) {
147 info->input_semantic_name[reg] = (ubyte)fulldecl->Semantic.Name;
148 info->input_semantic_index[reg] = (ubyte)fulldecl->Semantic.Index;
149 info->input_interpolate[reg] = (ubyte)fulldecl->Declaration.Interpolate;
150 info->input_centroid[reg] = (ubyte)fulldecl->Declaration.Centroid;
151 info->input_cylindrical_wrap[reg] = (ubyte)fulldecl->Declaration.CylindricalWrap;
152 info->num_inputs++;
153 }
154 else if (file == TGSI_FILE_OUTPUT) {
155 info->output_semantic_name[reg] = (ubyte)fulldecl->Semantic.Name;
156 info->output_semantic_index[reg] = (ubyte)fulldecl->Semantic.Index;
157 info->num_outputs++;
158
159 /* extra info for special outputs */
160 if (procType == TGSI_PROCESSOR_FRAGMENT &&
161 fulldecl->Semantic.Name == TGSI_SEMANTIC_POSITION)
162 info->writes_z = TRUE;
163 if (procType == TGSI_PROCESSOR_FRAGMENT &&
164 fulldecl->Semantic.Name == TGSI_SEMANTIC_STENCIL)
165 info->writes_stencil = TRUE;
166 if (procType == TGSI_PROCESSOR_VERTEX &&
167 fulldecl->Semantic.Name == TGSI_SEMANTIC_EDGEFLAG) {
168 info->writes_edgeflag = TRUE;
169 }
170 }
171
172 }
173 }
174 break;
175
176 case TGSI_TOKEN_TYPE_IMMEDIATE:
177 {
178 uint reg = info->immediate_count++;
179 uint file = TGSI_FILE_IMMEDIATE;
180
181 info->file_mask[file] |= (1 << reg);
182 info->file_count[file]++;
183 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
184 }
185 break;
186 case TGSI_TOKEN_TYPE_PROPERTY:
187 {
188 const struct tgsi_full_property *fullprop
189 = &parse.FullToken.FullProperty;
190
191 info->properties[info->num_properties].name =
192 fullprop->Property.PropertyName;
193 memcpy(info->properties[info->num_properties].data,
194 fullprop->u, 8 * sizeof(unsigned));;
195
196 ++info->num_properties;
197 }
198 break;
199
200 default:
201 assert( 0 );
202 }
203 }
204
205 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KIL] ||
206 info->opcode_count[TGSI_OPCODE_KILP]);
207
208 tgsi_parse_free (&parse);
209 }
210
211
212
213 /**
214 * Check if the given shader is a "passthrough" shader consisting of only
215 * MOV instructions of the form: MOV OUT[n], IN[n]
216 *
217 */
218 boolean
219 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
220 {
221 struct tgsi_parse_context parse;
222
223 /**
224 ** Setup to begin parsing input shader
225 **/
226 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
227 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
228 return FALSE;
229 }
230
231 /**
232 ** Loop over incoming program tokens/instructions
233 */
234 while (!tgsi_parse_end_of_tokens(&parse)) {
235
236 tgsi_parse_token(&parse);
237
238 switch (parse.FullToken.Token.Type) {
239 case TGSI_TOKEN_TYPE_INSTRUCTION:
240 {
241 struct tgsi_full_instruction *fullinst =
242 &parse.FullToken.FullInstruction;
243 const struct tgsi_full_src_register *src =
244 &fullinst->Src[0];
245 const struct tgsi_full_dst_register *dst =
246 &fullinst->Dst[0];
247
248 /* Do a whole bunch of checks for a simple move */
249 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
250 (src->Register.File != TGSI_FILE_INPUT &&
251 src->Register.File != TGSI_FILE_SYSTEM_VALUE) ||
252 dst->Register.File != TGSI_FILE_OUTPUT ||
253 src->Register.Index != dst->Register.Index ||
254
255 src->Register.Negate ||
256 src->Register.Absolute ||
257
258 src->Register.SwizzleX != TGSI_SWIZZLE_X ||
259 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
260 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
261 src->Register.SwizzleW != TGSI_SWIZZLE_W ||
262
263 dst->Register.WriteMask != TGSI_WRITEMASK_XYZW)
264 {
265 tgsi_parse_free(&parse);
266 return FALSE;
267 }
268 }
269 break;
270
271 case TGSI_TOKEN_TYPE_DECLARATION:
272 /* fall-through */
273 case TGSI_TOKEN_TYPE_IMMEDIATE:
274 /* fall-through */
275 case TGSI_TOKEN_TYPE_PROPERTY:
276 /* fall-through */
277 default:
278 ; /* no-op */
279 }
280 }
281
282 tgsi_parse_free(&parse);
283
284 /* if we get here, it's a pass-through shader */
285 return TRUE;
286 }