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