e6011d2d85a73fec30fb2620e76a42a6e39df2af
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_scan.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
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 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.
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 "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"
44
45
46
47
48 /**
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
52 */
53 void
54 tgsi_scan_shader(const struct tgsi_token *tokens,
55 struct tgsi_shader_info *info)
56 {
57 uint procType, i;
58 struct tgsi_parse_context parse;
59
60 memset(info, 0, sizeof(*info));
61 for (i = 0; i < TGSI_FILE_COUNT; i++)
62 info->file_max[i] = -1;
63 for (i = 0; i < Elements(info->const_file_max); i++)
64 info->const_file_max[i] = -1;
65
66 /**
67 ** Setup to begin parsing input shader
68 **/
69 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
70 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
71 return;
72 }
73 procType = parse.FullHeader.Processor.Processor;
74 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
75 procType == TGSI_PROCESSOR_VERTEX ||
76 procType == TGSI_PROCESSOR_GEOMETRY ||
77 procType == TGSI_PROCESSOR_COMPUTE);
78 info->processor = procType;
79
80
81 /**
82 ** Loop over incoming program tokens/instructions
83 */
84 while( !tgsi_parse_end_of_tokens( &parse ) ) {
85
86 info->num_tokens++;
87
88 tgsi_parse_token( &parse );
89
90 switch( parse.FullToken.Token.Type ) {
91 case TGSI_TOKEN_TYPE_INSTRUCTION:
92 {
93 const struct tgsi_full_instruction *fullinst
94 = &parse.FullToken.FullInstruction;
95 uint i;
96
97 assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
98 info->opcode_count[fullinst->Instruction.Opcode]++;
99
100 for (i = 0; i < fullinst->Instruction.NumSrcRegs; i++) {
101 const struct tgsi_full_src_register *src =
102 &fullinst->Src[i];
103 int ind = src->Register.Index;
104
105 /* Mark which inputs are effectively used */
106 if (src->Register.File == TGSI_FILE_INPUT) {
107 unsigned usage_mask;
108 usage_mask = tgsi_util_get_inst_usage_mask(fullinst, i);
109 if (src->Register.Indirect) {
110 for (ind = 0; ind < info->num_inputs; ++ind) {
111 info->input_usage_mask[ind] |= usage_mask;
112 }
113 } else {
114 assert(ind >= 0);
115 assert(ind < PIPE_MAX_SHADER_INPUTS);
116 info->input_usage_mask[ind] |= usage_mask;
117 }
118
119 if (procType == TGSI_PROCESSOR_FRAGMENT &&
120 info->reads_position &&
121 src->Register.Index == 0 &&
122 (src->Register.SwizzleX == TGSI_SWIZZLE_Z ||
123 src->Register.SwizzleY == TGSI_SWIZZLE_Z ||
124 src->Register.SwizzleZ == TGSI_SWIZZLE_Z ||
125 src->Register.SwizzleW == TGSI_SWIZZLE_Z)) {
126 info->reads_z = TRUE;
127 }
128 }
129
130 /* check for indirect register reads */
131 if (src->Register.Indirect) {
132 info->indirect_files |= (1 << src->Register.File);
133 info->indirect_files_read |= (1 << src->Register.File);
134 }
135
136 /* MSAA samplers */
137 if (src->Register.File == TGSI_FILE_SAMPLER) {
138 assert(fullinst->Instruction.Texture);
139 assert(src->Register.Index < Elements(info->is_msaa_sampler));
140
141 if (fullinst->Instruction.Texture &&
142 (fullinst->Texture.Texture == TGSI_TEXTURE_2D_MSAA ||
143 fullinst->Texture.Texture == TGSI_TEXTURE_2D_ARRAY_MSAA)) {
144 info->is_msaa_sampler[src->Register.Index] = TRUE;
145 }
146 }
147 }
148
149 /* check for indirect register writes */
150 for (i = 0; i < fullinst->Instruction.NumDstRegs; i++) {
151 const struct tgsi_full_dst_register *dst = &fullinst->Dst[i];
152 if (dst->Register.Indirect) {
153 info->indirect_files |= (1 << dst->Register.File);
154 info->indirect_files_written |= (1 << dst->Register.File);
155 }
156 }
157
158 info->num_instructions++;
159 }
160 break;
161
162 case TGSI_TOKEN_TYPE_DECLARATION:
163 {
164 const struct tgsi_full_declaration *fulldecl
165 = &parse.FullToken.FullDeclaration;
166 const uint file = fulldecl->Declaration.File;
167 uint reg;
168 if (fulldecl->Declaration.Array)
169 info->array_max[file] = MAX2(info->array_max[file], fulldecl->Array.ArrayID);
170 for (reg = fulldecl->Range.First;
171 reg <= fulldecl->Range.Last;
172 reg++) {
173 unsigned semName = fulldecl->Semantic.Name;
174 unsigned semIndex = fulldecl->Semantic.Index;
175
176 /* only first 32 regs will appear in this bitfield */
177 info->file_mask[file] |= (1 << reg);
178 info->file_count[file]++;
179 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
180
181 if (file == TGSI_FILE_CONSTANT) {
182 int buffer = 0;
183
184 if (fulldecl->Declaration.Dimension)
185 buffer = fulldecl->Dim.Index2D;
186
187 info->const_file_max[buffer] =
188 MAX2(info->const_file_max[buffer], (int)reg);
189 }
190 else if (file == TGSI_FILE_INPUT) {
191 info->input_semantic_name[reg] = (ubyte) semName;
192 info->input_semantic_index[reg] = (ubyte) semIndex;
193 info->input_interpolate[reg] = (ubyte)fulldecl->Interp.Interpolate;
194 info->input_interpolate_loc[reg] = (ubyte)fulldecl->Interp.Location;
195 info->input_cylindrical_wrap[reg] = (ubyte)fulldecl->Interp.CylindricalWrap;
196 info->num_inputs++;
197
198 if (fulldecl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
199 info->uses_centroid = TRUE;
200
201 if (semName == TGSI_SEMANTIC_PRIMID)
202 info->uses_primid = TRUE;
203 else if (procType == TGSI_PROCESSOR_FRAGMENT) {
204 if (semName == TGSI_SEMANTIC_POSITION)
205 info->reads_position = TRUE;
206 else if (semName == TGSI_SEMANTIC_FACE)
207 info->uses_frontface = TRUE;
208 }
209 }
210 else if (file == TGSI_FILE_SYSTEM_VALUE) {
211 unsigned index = fulldecl->Range.First;
212
213 info->system_value_semantic_name[index] = semName;
214 info->num_system_values = MAX2(info->num_system_values,
215 index + 1);
216
217 if (semName == TGSI_SEMANTIC_INSTANCEID) {
218 info->uses_instanceid = TRUE;
219 }
220 else if (semName == TGSI_SEMANTIC_VERTEXID) {
221 info->uses_vertexid = TRUE;
222 }
223 else if (semName == TGSI_SEMANTIC_VERTEXID_NOBASE) {
224 info->uses_vertexid_nobase = TRUE;
225 }
226 else if (semName == TGSI_SEMANTIC_BASEVERTEX) {
227 info->uses_basevertex = TRUE;
228 }
229 else if (semName == TGSI_SEMANTIC_PRIMID) {
230 info->uses_primid = TRUE;
231 }
232 }
233 else if (file == TGSI_FILE_OUTPUT) {
234 info->output_semantic_name[reg] = (ubyte) semName;
235 info->output_semantic_index[reg] = (ubyte) semIndex;
236 info->num_outputs++;
237
238 if (procType == TGSI_PROCESSOR_VERTEX ||
239 procType == TGSI_PROCESSOR_GEOMETRY) {
240 if (semName == TGSI_SEMANTIC_CLIPDIST) {
241 info->num_written_clipdistance +=
242 util_bitcount(fulldecl->Declaration.UsageMask);
243 info->clipdist_writemask |=
244 fulldecl->Declaration.UsageMask << (semIndex*4);
245 }
246 else if (semName == TGSI_SEMANTIC_CULLDIST) {
247 info->num_written_culldistance +=
248 util_bitcount(fulldecl->Declaration.UsageMask);
249 info->culldist_writemask |=
250 fulldecl->Declaration.UsageMask << (semIndex*4);
251 }
252 else if (semName == TGSI_SEMANTIC_VIEWPORT_INDEX) {
253 info->writes_viewport_index = TRUE;
254 }
255 else if (semName == TGSI_SEMANTIC_LAYER) {
256 info->writes_layer = TRUE;
257 }
258 else if (semName == TGSI_SEMANTIC_PSIZE) {
259 info->writes_psize = TRUE;
260 }
261 else if (semName == TGSI_SEMANTIC_CLIPVERTEX) {
262 info->writes_clipvertex = TRUE;
263 }
264 }
265
266 if (procType == TGSI_PROCESSOR_FRAGMENT) {
267 if (semName == TGSI_SEMANTIC_POSITION) {
268 info->writes_z = TRUE;
269 }
270 else if (semName == TGSI_SEMANTIC_STENCIL) {
271 info->writes_stencil = TRUE;
272 }
273 }
274
275 if (procType == TGSI_PROCESSOR_VERTEX) {
276 if (semName == TGSI_SEMANTIC_EDGEFLAG) {
277 info->writes_edgeflag = TRUE;
278 }
279 }
280 }
281 }
282 }
283 break;
284
285 case TGSI_TOKEN_TYPE_IMMEDIATE:
286 {
287 uint reg = info->immediate_count++;
288 uint file = TGSI_FILE_IMMEDIATE;
289
290 info->file_mask[file] |= (1 << reg);
291 info->file_count[file]++;
292 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
293 }
294 break;
295
296 case TGSI_TOKEN_TYPE_PROPERTY:
297 {
298 const struct tgsi_full_property *fullprop
299 = &parse.FullToken.FullProperty;
300 unsigned name = fullprop->Property.PropertyName;
301
302 assert(name < Elements(info->properties));
303 info->properties[name] = fullprop->u[0].Data;
304 }
305 break;
306
307 default:
308 assert( 0 );
309 }
310 }
311
312 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KILL_IF] ||
313 info->opcode_count[TGSI_OPCODE_KILL]);
314
315 /* The dimensions of the IN decleration in geometry shader have
316 * to be deduced from the type of the input primitive.
317 */
318 if (procType == TGSI_PROCESSOR_GEOMETRY) {
319 unsigned input_primitive =
320 info->properties[TGSI_PROPERTY_GS_INPUT_PRIM];
321 int num_verts = u_vertices_per_prim(input_primitive);
322 int j;
323 info->file_count[TGSI_FILE_INPUT] = num_verts;
324 info->file_max[TGSI_FILE_INPUT] =
325 MAX2(info->file_max[TGSI_FILE_INPUT], num_verts - 1);
326 for (j = 0; j < num_verts; ++j) {
327 info->file_mask[TGSI_FILE_INPUT] |= (1 << j);
328 }
329 }
330
331 tgsi_parse_free (&parse);
332 }
333
334
335
336 /**
337 * Check if the given shader is a "passthrough" shader consisting of only
338 * MOV instructions of the form: MOV OUT[n], IN[n]
339 *
340 */
341 boolean
342 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
343 {
344 struct tgsi_parse_context parse;
345
346 /**
347 ** Setup to begin parsing input shader
348 **/
349 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
350 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
351 return FALSE;
352 }
353
354 /**
355 ** Loop over incoming program tokens/instructions
356 */
357 while (!tgsi_parse_end_of_tokens(&parse)) {
358
359 tgsi_parse_token(&parse);
360
361 switch (parse.FullToken.Token.Type) {
362 case TGSI_TOKEN_TYPE_INSTRUCTION:
363 {
364 struct tgsi_full_instruction *fullinst =
365 &parse.FullToken.FullInstruction;
366 const struct tgsi_full_src_register *src =
367 &fullinst->Src[0];
368 const struct tgsi_full_dst_register *dst =
369 &fullinst->Dst[0];
370
371 /* Do a whole bunch of checks for a simple move */
372 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
373 (src->Register.File != TGSI_FILE_INPUT &&
374 src->Register.File != TGSI_FILE_SYSTEM_VALUE) ||
375 dst->Register.File != TGSI_FILE_OUTPUT ||
376 src->Register.Index != dst->Register.Index ||
377
378 src->Register.Negate ||
379 src->Register.Absolute ||
380
381 src->Register.SwizzleX != TGSI_SWIZZLE_X ||
382 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
383 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
384 src->Register.SwizzleW != TGSI_SWIZZLE_W ||
385
386 dst->Register.WriteMask != TGSI_WRITEMASK_XYZW)
387 {
388 tgsi_parse_free(&parse);
389 return FALSE;
390 }
391 }
392 break;
393
394 case TGSI_TOKEN_TYPE_DECLARATION:
395 /* fall-through */
396 case TGSI_TOKEN_TYPE_IMMEDIATE:
397 /* fall-through */
398 case TGSI_TOKEN_TYPE_PROPERTY:
399 /* fall-through */
400 default:
401 ; /* no-op */
402 }
403 }
404
405 tgsi_parse_free(&parse);
406
407 /* if we get here, it's a pass-through shader */
408 return TRUE;
409 }