tgsi: keep track of read vs written indirects
[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 for (reg = fulldecl->Range.First;
169 reg <= fulldecl->Range.Last;
170 reg++) {
171 unsigned semName = fulldecl->Semantic.Name;
172 unsigned semIndex = fulldecl->Semantic.Index;
173
174 /* only first 32 regs will appear in this bitfield */
175 info->file_mask[file] |= (1 << reg);
176 info->file_count[file]++;
177 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
178
179 if (file == TGSI_FILE_CONSTANT) {
180 int buffer = 0;
181
182 if (fulldecl->Declaration.Dimension)
183 buffer = fulldecl->Dim.Index2D;
184
185 info->const_file_max[buffer] =
186 MAX2(info->const_file_max[buffer], (int)reg);
187 }
188 else if (file == TGSI_FILE_INPUT) {
189 info->input_semantic_name[reg] = (ubyte) semName;
190 info->input_semantic_index[reg] = (ubyte) semIndex;
191 info->input_interpolate[reg] = (ubyte)fulldecl->Interp.Interpolate;
192 info->input_interpolate_loc[reg] = (ubyte)fulldecl->Interp.Location;
193 info->input_cylindrical_wrap[reg] = (ubyte)fulldecl->Interp.CylindricalWrap;
194 info->num_inputs++;
195
196 if (fulldecl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
197 info->uses_centroid = TRUE;
198
199 if (semName == TGSI_SEMANTIC_PRIMID)
200 info->uses_primid = TRUE;
201 else if (procType == TGSI_PROCESSOR_FRAGMENT) {
202 if (semName == TGSI_SEMANTIC_POSITION)
203 info->reads_position = TRUE;
204 else if (semName == TGSI_SEMANTIC_FACE)
205 info->uses_frontface = TRUE;
206 }
207 }
208 else if (file == TGSI_FILE_SYSTEM_VALUE) {
209 unsigned index = fulldecl->Range.First;
210
211 info->system_value_semantic_name[index] = semName;
212 info->num_system_values = MAX2(info->num_system_values,
213 index + 1);
214
215 if (semName == TGSI_SEMANTIC_INSTANCEID) {
216 info->uses_instanceid = TRUE;
217 }
218 else if (semName == TGSI_SEMANTIC_VERTEXID) {
219 info->uses_vertexid = TRUE;
220 }
221 else if (semName == TGSI_SEMANTIC_VERTEXID_NOBASE) {
222 info->uses_vertexid_nobase = TRUE;
223 }
224 else if (semName == TGSI_SEMANTIC_BASEVERTEX) {
225 info->uses_basevertex = TRUE;
226 }
227 else if (semName == TGSI_SEMANTIC_PRIMID) {
228 info->uses_primid = TRUE;
229 }
230 }
231 else if (file == TGSI_FILE_OUTPUT) {
232 info->output_semantic_name[reg] = (ubyte) semName;
233 info->output_semantic_index[reg] = (ubyte) semIndex;
234 info->num_outputs++;
235
236 if (procType == TGSI_PROCESSOR_VERTEX ||
237 procType == TGSI_PROCESSOR_GEOMETRY) {
238 if (semName == TGSI_SEMANTIC_CLIPDIST) {
239 info->num_written_clipdistance +=
240 util_bitcount(fulldecl->Declaration.UsageMask);
241 info->clipdist_writemask |=
242 fulldecl->Declaration.UsageMask << (semIndex*4);
243 }
244 else if (semName == TGSI_SEMANTIC_CULLDIST) {
245 info->num_written_culldistance +=
246 util_bitcount(fulldecl->Declaration.UsageMask);
247 info->culldist_writemask |=
248 fulldecl->Declaration.UsageMask << (semIndex*4);
249 }
250 else if (semName == TGSI_SEMANTIC_VIEWPORT_INDEX) {
251 info->writes_viewport_index = TRUE;
252 }
253 else if (semName == TGSI_SEMANTIC_LAYER) {
254 info->writes_layer = TRUE;
255 }
256 else if (semName == TGSI_SEMANTIC_PSIZE) {
257 info->writes_psize = TRUE;
258 }
259 else if (semName == TGSI_SEMANTIC_CLIPVERTEX) {
260 info->writes_clipvertex = TRUE;
261 }
262 }
263
264 if (procType == TGSI_PROCESSOR_FRAGMENT) {
265 if (semName == TGSI_SEMANTIC_POSITION) {
266 info->writes_z = TRUE;
267 }
268 else if (semName == TGSI_SEMANTIC_STENCIL) {
269 info->writes_stencil = TRUE;
270 }
271 }
272
273 if (procType == TGSI_PROCESSOR_VERTEX) {
274 if (semName == TGSI_SEMANTIC_EDGEFLAG) {
275 info->writes_edgeflag = TRUE;
276 }
277 }
278 }
279 }
280 }
281 break;
282
283 case TGSI_TOKEN_TYPE_IMMEDIATE:
284 {
285 uint reg = info->immediate_count++;
286 uint file = TGSI_FILE_IMMEDIATE;
287
288 info->file_mask[file] |= (1 << reg);
289 info->file_count[file]++;
290 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
291 }
292 break;
293
294 case TGSI_TOKEN_TYPE_PROPERTY:
295 {
296 const struct tgsi_full_property *fullprop
297 = &parse.FullToken.FullProperty;
298 unsigned name = fullprop->Property.PropertyName;
299
300 assert(name < Elements(info->properties));
301 info->properties[name] = fullprop->u[0].Data;
302 }
303 break;
304
305 default:
306 assert( 0 );
307 }
308 }
309
310 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KILL_IF] ||
311 info->opcode_count[TGSI_OPCODE_KILL]);
312
313 /* The dimensions of the IN decleration in geometry shader have
314 * to be deduced from the type of the input primitive.
315 */
316 if (procType == TGSI_PROCESSOR_GEOMETRY) {
317 unsigned input_primitive =
318 info->properties[TGSI_PROPERTY_GS_INPUT_PRIM];
319 int num_verts = u_vertices_per_prim(input_primitive);
320 int j;
321 info->file_count[TGSI_FILE_INPUT] = num_verts;
322 info->file_max[TGSI_FILE_INPUT] =
323 MAX2(info->file_max[TGSI_FILE_INPUT], num_verts - 1);
324 for (j = 0; j < num_verts; ++j) {
325 info->file_mask[TGSI_FILE_INPUT] |= (1 << j);
326 }
327 }
328
329 tgsi_parse_free (&parse);
330 }
331
332
333
334 /**
335 * Check if the given shader is a "passthrough" shader consisting of only
336 * MOV instructions of the form: MOV OUT[n], IN[n]
337 *
338 */
339 boolean
340 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
341 {
342 struct tgsi_parse_context parse;
343
344 /**
345 ** Setup to begin parsing input shader
346 **/
347 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
348 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
349 return FALSE;
350 }
351
352 /**
353 ** Loop over incoming program tokens/instructions
354 */
355 while (!tgsi_parse_end_of_tokens(&parse)) {
356
357 tgsi_parse_token(&parse);
358
359 switch (parse.FullToken.Token.Type) {
360 case TGSI_TOKEN_TYPE_INSTRUCTION:
361 {
362 struct tgsi_full_instruction *fullinst =
363 &parse.FullToken.FullInstruction;
364 const struct tgsi_full_src_register *src =
365 &fullinst->Src[0];
366 const struct tgsi_full_dst_register *dst =
367 &fullinst->Dst[0];
368
369 /* Do a whole bunch of checks for a simple move */
370 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
371 (src->Register.File != TGSI_FILE_INPUT &&
372 src->Register.File != TGSI_FILE_SYSTEM_VALUE) ||
373 dst->Register.File != TGSI_FILE_OUTPUT ||
374 src->Register.Index != dst->Register.Index ||
375
376 src->Register.Negate ||
377 src->Register.Absolute ||
378
379 src->Register.SwizzleX != TGSI_SWIZZLE_X ||
380 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
381 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
382 src->Register.SwizzleW != TGSI_SWIZZLE_W ||
383
384 dst->Register.WriteMask != TGSI_WRITEMASK_XYZW)
385 {
386 tgsi_parse_free(&parse);
387 return FALSE;
388 }
389 }
390 break;
391
392 case TGSI_TOKEN_TYPE_DECLARATION:
393 /* fall-through */
394 case TGSI_TOKEN_TYPE_IMMEDIATE:
395 /* fall-through */
396 case TGSI_TOKEN_TYPE_PROPERTY:
397 /* fall-through */
398 default:
399 ; /* no-op */
400 }
401 }
402
403 tgsi_parse_free(&parse);
404
405 /* if we get here, it's a pass-through shader */
406 return TRUE;
407 }