tgsi: fix detection of front-facing attribute
[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_math.h"
38 #include "tgsi/tgsi_build.h"
39 #include "tgsi/tgsi_parse.h"
40 #include "tgsi/tgsi_scan.h"
41
42
43
44
45 /**
46 * Scan the given TGSI shader to collect information such as number of
47 * registers used, special instructions used, etc.
48 * \return info the result of the scan
49 */
50 void
51 tgsi_scan_shader(const struct tgsi_token *tokens,
52 struct tgsi_shader_info *info)
53 {
54 uint procType, i;
55 struct tgsi_parse_context parse;
56
57 memset(info, 0, sizeof(*info));
58 for (i = 0; i < TGSI_FILE_COUNT; i++)
59 info->file_max[i] = -1;
60
61 /**
62 ** Setup to begin parsing input shader
63 **/
64 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
65 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
66 return;
67 }
68 procType = parse.FullHeader.Processor.Processor;
69 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
70 procType == TGSI_PROCESSOR_VERTEX ||
71 procType == TGSI_PROCESSOR_GEOMETRY);
72
73
74 /**
75 ** Loop over incoming program tokens/instructions
76 */
77 while( !tgsi_parse_end_of_tokens( &parse ) ) {
78
79 info->num_tokens++;
80
81 tgsi_parse_token( &parse );
82
83 switch( parse.FullToken.Token.Type ) {
84 case TGSI_TOKEN_TYPE_INSTRUCTION:
85 {
86 const struct tgsi_full_instruction *fullinst
87 = &parse.FullToken.FullInstruction;
88
89 assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
90 info->opcode_count[fullinst->Instruction.Opcode]++;
91
92 /* special case: scan fragment shaders for use of the fog
93 * input/attribute. The X component is fog, the Y component
94 * is the front/back-face flag.
95 */
96 if (procType == TGSI_PROCESSOR_FRAGMENT) {
97 uint i;
98 for (i = 0; i < fullinst->Instruction.NumSrcRegs; i++) {
99 const struct tgsi_full_src_register *src =
100 &fullinst->FullSrcRegisters[i];
101 if (src->SrcRegister.File == TGSI_FILE_INPUT) {
102 const int ind = src->SrcRegister.Index;
103 if (info->input_semantic_name[ind] == TGSI_SEMANTIC_FOG) {
104 info->uses_fogcoord = TRUE;
105 }
106 else if (info->input_semantic_name[ind] == TGSI_SEMANTIC_FACE) {
107 info->uses_frontfacing = TRUE;
108 }
109 }
110 }
111 }
112 }
113 break;
114
115 case TGSI_TOKEN_TYPE_DECLARATION:
116 {
117 const struct tgsi_full_declaration *fulldecl
118 = &parse.FullToken.FullDeclaration;
119 const uint file = fulldecl->Declaration.File;
120 uint reg;
121 for (reg = fulldecl->DeclarationRange.First;
122 reg <= fulldecl->DeclarationRange.Last;
123 reg++) {
124
125 /* only first 32 regs will appear in this bitfield */
126 info->file_mask[file] |= (1 << reg);
127 info->file_count[file]++;
128 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
129
130 if (file == TGSI_FILE_INPUT) {
131 info->input_semantic_name[reg] = (ubyte)fulldecl->Semantic.SemanticName;
132 info->input_semantic_index[reg] = (ubyte)fulldecl->Semantic.SemanticIndex;
133 info->input_interpolate[reg] = (ubyte)fulldecl->Declaration.Interpolate;
134 info->num_inputs++;
135 }
136 else if (file == TGSI_FILE_OUTPUT) {
137 info->output_semantic_name[reg] = (ubyte)fulldecl->Semantic.SemanticName;
138 info->output_semantic_index[reg] = (ubyte)fulldecl->Semantic.SemanticIndex;
139 info->num_outputs++;
140 }
141
142 /* special case */
143 if (procType == TGSI_PROCESSOR_FRAGMENT &&
144 file == TGSI_FILE_OUTPUT &&
145 fulldecl->Semantic.SemanticName == TGSI_SEMANTIC_POSITION) {
146 info->writes_z = TRUE;
147 }
148 }
149 }
150 break;
151
152 case TGSI_TOKEN_TYPE_IMMEDIATE:
153 {
154 uint reg = info->immediate_count++;
155 uint file = TGSI_FILE_IMMEDIATE;
156
157 info->file_mask[file] |= (1 << reg);
158 info->file_count[file]++;
159 info->file_max[file] = MAX2(info->file_max[file], (int)reg);
160 }
161 break;
162
163 default:
164 assert( 0 );
165 }
166 }
167
168 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KIL] ||
169 info->opcode_count[TGSI_OPCODE_KILP]);
170
171 tgsi_parse_free (&parse);
172 }
173
174
175
176 /**
177 * Check if the given shader is a "passthrough" shader consisting of only
178 * MOV instructions of the form: MOV OUT[n], IN[n]
179 *
180 */
181 boolean
182 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
183 {
184 struct tgsi_parse_context parse;
185
186 /**
187 ** Setup to begin parsing input shader
188 **/
189 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
190 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
191 return FALSE;
192 }
193
194 /**
195 ** Loop over incoming program tokens/instructions
196 */
197 while (!tgsi_parse_end_of_tokens(&parse)) {
198
199 tgsi_parse_token(&parse);
200
201 switch (parse.FullToken.Token.Type) {
202 case TGSI_TOKEN_TYPE_INSTRUCTION:
203 {
204 struct tgsi_full_instruction *fullinst =
205 &parse.FullToken.FullInstruction;
206 const struct tgsi_full_src_register *src =
207 &fullinst->FullSrcRegisters[0];
208 const struct tgsi_full_dst_register *dst =
209 &fullinst->FullDstRegisters[0];
210
211 /* Do a whole bunch of checks for a simple move */
212 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
213 src->SrcRegister.File != TGSI_FILE_INPUT ||
214 dst->DstRegister.File != TGSI_FILE_OUTPUT ||
215 src->SrcRegister.Index != dst->DstRegister.Index ||
216
217 src->SrcRegister.Negate ||
218 src->SrcRegisterExtMod.Negate ||
219 src->SrcRegisterExtMod.Absolute ||
220 src->SrcRegisterExtMod.Scale2X ||
221 src->SrcRegisterExtMod.Bias ||
222 src->SrcRegisterExtMod.Complement ||
223
224 src->SrcRegister.SwizzleX != TGSI_SWIZZLE_X ||
225 src->SrcRegister.SwizzleY != TGSI_SWIZZLE_Y ||
226 src->SrcRegister.SwizzleZ != TGSI_SWIZZLE_Z ||
227 src->SrcRegister.SwizzleW != TGSI_SWIZZLE_W ||
228
229 dst->DstRegister.WriteMask != TGSI_WRITEMASK_XYZW)
230 {
231 tgsi_parse_free(&parse);
232 return FALSE;
233 }
234 }
235 break;
236
237 case TGSI_TOKEN_TYPE_DECLARATION:
238 /* fall-through */
239 case TGSI_TOKEN_TYPE_IMMEDIATE:
240 /* fall-through */
241 default:
242 ; /* no-op */
243 }
244 }
245
246 tgsi_parse_free(&parse);
247
248 /* if we get here, it's a pass-through shader */
249 return TRUE;
250 }