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