gallium: Add a new clip_halfz rasterizer state.
[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_debug.h"
38 #include "util/u_math.h"
39 #include "tgsi/tgsi_parse.h"
40 #include "tgsi/tgsi_util.h"
41 #include "tgsi/tgsi_scan.h"
42
43
44
45
46 /**
47 * Scan the given TGSI shader to collect information such as number of
48 * registers used, special instructions used, etc.
49 * \return info the result of the scan
50 */
51 void
52 tgsi_scan_shader(const struct tgsi_token *tokens,
53 struct tgsi_shader_info *info)
54 {
55 uint procType, i;
56 struct tgsi_parse_context parse;
57
58 memset(info, 0, sizeof(*info));
59 for (i = 0; i < TGSI_FILE_COUNT; i++)
60 info->file_max[i] = -1;
61
62 /**
63 ** Setup to begin parsing input shader
64 **/
65 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
66 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
67 return;
68 }
69 procType = parse.FullHeader.Processor.Processor;
70 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
71 procType == TGSI_PROCESSOR_VERTEX ||
72 procType == TGSI_PROCESSOR_GEOMETRY ||
73 procType == TGSI_PROCESSOR_COMPUTE);
74
75
76 /**
77 ** Loop over incoming program tokens/instructions
78 */
79 while( !tgsi_parse_end_of_tokens( &parse ) ) {
80
81 info->num_tokens++;
82
83 tgsi_parse_token( &parse );
84
85 switch( parse.FullToken.Token.Type ) {
86 case TGSI_TOKEN_TYPE_INSTRUCTION:
87 {
88 const struct tgsi_full_instruction *fullinst
89 = &parse.FullToken.FullInstruction;
90 uint i;
91
92 assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
93 info->opcode_count[fullinst->Instruction.Opcode]++;
94
95 for (i = 0; i < fullinst->Instruction.NumSrcRegs; i++) {
96 const struct tgsi_full_src_register *src =
97 &fullinst->Src[i];
98 int ind = src->Register.Index;
99
100 /* Mark which inputs are effectively used */
101 if (src->Register.File == TGSI_FILE_INPUT) {
102 unsigned usage_mask;
103 usage_mask = tgsi_util_get_inst_usage_mask(fullinst, i);
104 if (src->Register.Indirect) {
105 for (ind = 0; ind < info->num_inputs; ++ind) {
106 info->input_usage_mask[ind] |= usage_mask;
107 }
108 } else {
109 assert(ind >= 0);
110 assert(ind < PIPE_MAX_SHADER_INPUTS);
111 info->input_usage_mask[ind] |= usage_mask;
112 }
113
114 if (procType == TGSI_PROCESSOR_FRAGMENT &&
115 src->Register.File == TGSI_FILE_INPUT &&
116 info->reads_position &&
117 src->Register.Index == 0 &&
118 (src->Register.SwizzleX == TGSI_SWIZZLE_Z ||
119 src->Register.SwizzleY == TGSI_SWIZZLE_Z ||
120 src->Register.SwizzleZ == TGSI_SWIZZLE_Z ||
121 src->Register.SwizzleW == TGSI_SWIZZLE_Z)) {
122 info->reads_z = TRUE;
123 }
124 }
125
126 /* check for indirect register reads */
127 if (src->Register.Indirect) {
128 info->indirect_files |= (1 << src->Register.File);
129 }
130 }
131
132 /* check for indirect register writes */
133 for (i = 0; i < fullinst->Instruction.NumDstRegs; i++) {
134 const struct tgsi_full_dst_register *dst = &fullinst->Dst[i];
135 if (dst->Register.Indirect) {
136 info->indirect_files |= (1 << dst->Register.File);
137 }
138 }
139
140 info->num_instructions++;
141 }
142 break;
143
144 case TGSI_TOKEN_TYPE_DECLARATION:
145 {
146 const struct tgsi_full_declaration *fulldecl
147 = &parse.FullToken.FullDeclaration;
148 const uint file = fulldecl->Declaration.File;
149 uint reg;
150 for (reg = fulldecl->Range.First;
151 reg <= fulldecl->Range.Last;
152 reg++) {
153
154 /* only first 32 regs will appear in this bitfield */
155 info->file_mask[file] |= (1 << reg);
156 info->file_count[file]++;
157 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
158
159 if (file == TGSI_FILE_INPUT) {
160 info->input_semantic_name[reg] = (ubyte)fulldecl->Semantic.Name;
161 info->input_semantic_index[reg] = (ubyte)fulldecl->Semantic.Index;
162 info->input_interpolate[reg] = (ubyte)fulldecl->Interp.Interpolate;
163 info->input_centroid[reg] = (ubyte)fulldecl->Interp.Centroid;
164 info->input_cylindrical_wrap[reg] = (ubyte)fulldecl->Interp.CylindricalWrap;
165 info->num_inputs++;
166
167 if (procType == TGSI_PROCESSOR_FRAGMENT &&
168 fulldecl->Semantic.Name == TGSI_SEMANTIC_POSITION)
169 info->reads_position = TRUE;
170 }
171 else if (file == TGSI_FILE_SYSTEM_VALUE) {
172 unsigned index = fulldecl->Range.First;
173 unsigned semName = fulldecl->Semantic.Name;
174
175 info->system_value_semantic_name[index] = semName;
176 info->num_system_values = MAX2(info->num_system_values,
177 index + 1);
178
179 /*
180 info->system_value_semantic_name[info->num_system_values++] =
181 fulldecl->Semantic.Name;
182 */
183
184 if (fulldecl->Semantic.Name == TGSI_SEMANTIC_INSTANCEID) {
185 info->uses_instanceid = TRUE;
186 }
187 else if (fulldecl->Semantic.Name == TGSI_SEMANTIC_VERTEXID) {
188 info->uses_vertexid = TRUE;
189 } else if (fulldecl->Semantic.Name == TGSI_SEMANTIC_PRIMID) {
190 info->uses_primid = TRUE;
191 }
192 }
193 else if (file == TGSI_FILE_OUTPUT) {
194 info->output_semantic_name[reg] = (ubyte)fulldecl->Semantic.Name;
195 info->output_semantic_index[reg] = (ubyte)fulldecl->Semantic.Index;
196 info->num_outputs++;
197
198 if (procType == TGSI_PROCESSOR_VERTEX &&
199 fulldecl->Semantic.Name == TGSI_SEMANTIC_CLIPDIST) {
200 info->num_written_clipdistance += util_bitcount(fulldecl->Declaration.UsageMask);
201 }
202 /* extra info for special outputs */
203 if (procType == TGSI_PROCESSOR_FRAGMENT &&
204 fulldecl->Semantic.Name == TGSI_SEMANTIC_POSITION)
205 info->writes_z = TRUE;
206 if (procType == TGSI_PROCESSOR_FRAGMENT &&
207 fulldecl->Semantic.Name == TGSI_SEMANTIC_STENCIL)
208 info->writes_stencil = TRUE;
209 if (procType == TGSI_PROCESSOR_VERTEX &&
210 fulldecl->Semantic.Name == TGSI_SEMANTIC_EDGEFLAG) {
211 info->writes_edgeflag = TRUE;
212 }
213 }
214
215 }
216 }
217 break;
218
219 case TGSI_TOKEN_TYPE_IMMEDIATE:
220 {
221 uint reg = info->immediate_count++;
222 uint file = TGSI_FILE_IMMEDIATE;
223
224 info->file_mask[file] |= (1 << reg);
225 info->file_count[file]++;
226 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
227 }
228 break;
229
230 case TGSI_TOKEN_TYPE_PROPERTY:
231 {
232 const struct tgsi_full_property *fullprop
233 = &parse.FullToken.FullProperty;
234
235 info->properties[info->num_properties].name =
236 fullprop->Property.PropertyName;
237 memcpy(info->properties[info->num_properties].data,
238 fullprop->u, 8 * sizeof(unsigned));;
239
240 ++info->num_properties;
241 }
242 break;
243
244 default:
245 assert( 0 );
246 }
247 }
248
249 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KIL] ||
250 info->opcode_count[TGSI_OPCODE_KILP]);
251
252 /* extract simple properties */
253 for (i = 0; i < info->num_properties; ++i) {
254 switch (info->properties[i].name) {
255 case TGSI_PROPERTY_FS_COORD_ORIGIN:
256 info->origin_lower_left = info->properties[i].data[0];
257 break;
258 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER:
259 info->pixel_center_integer = info->properties[i].data[0];
260 break;
261 case TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS:
262 info->color0_writes_all_cbufs = info->properties[i].data[0];
263 break;
264 default:
265 ;
266 }
267 }
268
269 tgsi_parse_free (&parse);
270 }
271
272
273
274 /**
275 * Check if the given shader is a "passthrough" shader consisting of only
276 * MOV instructions of the form: MOV OUT[n], IN[n]
277 *
278 */
279 boolean
280 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
281 {
282 struct tgsi_parse_context parse;
283
284 /**
285 ** Setup to begin parsing input shader
286 **/
287 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
288 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
289 return FALSE;
290 }
291
292 /**
293 ** Loop over incoming program tokens/instructions
294 */
295 while (!tgsi_parse_end_of_tokens(&parse)) {
296
297 tgsi_parse_token(&parse);
298
299 switch (parse.FullToken.Token.Type) {
300 case TGSI_TOKEN_TYPE_INSTRUCTION:
301 {
302 struct tgsi_full_instruction *fullinst =
303 &parse.FullToken.FullInstruction;
304 const struct tgsi_full_src_register *src =
305 &fullinst->Src[0];
306 const struct tgsi_full_dst_register *dst =
307 &fullinst->Dst[0];
308
309 /* Do a whole bunch of checks for a simple move */
310 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
311 (src->Register.File != TGSI_FILE_INPUT &&
312 src->Register.File != TGSI_FILE_SYSTEM_VALUE) ||
313 dst->Register.File != TGSI_FILE_OUTPUT ||
314 src->Register.Index != dst->Register.Index ||
315
316 src->Register.Negate ||
317 src->Register.Absolute ||
318
319 src->Register.SwizzleX != TGSI_SWIZZLE_X ||
320 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
321 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
322 src->Register.SwizzleW != TGSI_SWIZZLE_W ||
323
324 dst->Register.WriteMask != TGSI_WRITEMASK_XYZW)
325 {
326 tgsi_parse_free(&parse);
327 return FALSE;
328 }
329 }
330 break;
331
332 case TGSI_TOKEN_TYPE_DECLARATION:
333 /* fall-through */
334 case TGSI_TOKEN_TYPE_IMMEDIATE:
335 /* fall-through */
336 case TGSI_TOKEN_TYPE_PROPERTY:
337 /* fall-through */
338 default:
339 ; /* no-op */
340 }
341 }
342
343 tgsi_parse_free(&parse);
344
345 /* if we get here, it's a pass-through shader */
346 return TRUE;
347 }