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