Merge branch 'master' into gallium-0.2
[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 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * TGSI program scan utility.
30 * Used to determine which registers and instructions are used by a shader.
31 *
32 * Authors: Brian Paul
33 */
34
35
36 #include "util/u_math.h"
37 #include "tgsi/tgsi_build.h"
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_scan.h"
40
41
42
43
44 /**
45 */
46 void
47 tgsi_scan_shader(const struct tgsi_token *tokens,
48 struct tgsi_shader_info *info)
49 {
50 uint procType, i;
51 struct tgsi_parse_context parse;
52
53 memset(info, 0, sizeof(*info));
54 for (i = 0; i < TGSI_FILE_COUNT; i++)
55 info->file_max[i] = -1;
56
57 /**
58 ** Setup to begin parsing input shader
59 **/
60 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
61 debug_printf("tgsi_parse_init() failed in tgsi_scan_shader()!\n");
62 return;
63 }
64 procType = parse.FullHeader.Processor.Processor;
65 assert(procType == TGSI_PROCESSOR_FRAGMENT ||
66 procType == TGSI_PROCESSOR_VERTEX ||
67 procType == TGSI_PROCESSOR_GEOMETRY);
68
69
70 /**
71 ** Loop over incoming program tokens/instructions
72 */
73 while( !tgsi_parse_end_of_tokens( &parse ) ) {
74
75 info->num_tokens++;
76
77 tgsi_parse_token( &parse );
78
79 switch( parse.FullToken.Token.Type ) {
80 case TGSI_TOKEN_TYPE_INSTRUCTION:
81 {
82 struct tgsi_full_instruction *fullinst
83 = &parse.FullToken.FullInstruction;
84
85 assert(fullinst->Instruction.Opcode < TGSI_OPCODE_LAST);
86 info->opcode_count[fullinst->Instruction.Opcode]++;
87 }
88 break;
89
90 case TGSI_TOKEN_TYPE_DECLARATION:
91 {
92 struct tgsi_full_declaration *fulldecl
93 = &parse.FullToken.FullDeclaration;
94 uint file = fulldecl->Declaration.File;
95 uint i;
96 for (i = fulldecl->DeclarationRange.First;
97 i <= fulldecl->DeclarationRange.Last;
98 i++) {
99
100 /* only first 32 regs will appear in this bitfield */
101 info->file_mask[file] |= (1 << i);
102 info->file_count[file]++;
103 info->file_max[file] = MAX2(info->file_max[file], (int)i);
104
105 if (file == TGSI_FILE_INPUT) {
106 info->input_semantic_name[i] = (ubyte)fulldecl->Semantic.SemanticName;
107 info->input_semantic_index[i] = (ubyte)fulldecl->Semantic.SemanticIndex;
108 info->num_inputs++;
109 }
110
111 if (file == TGSI_FILE_OUTPUT) {
112 info->output_semantic_name[i] = (ubyte)fulldecl->Semantic.SemanticName;
113 info->output_semantic_index[i] = (ubyte)fulldecl->Semantic.SemanticIndex;
114 info->num_outputs++;
115 }
116
117 /* special case */
118 if (procType == TGSI_PROCESSOR_FRAGMENT &&
119 file == TGSI_FILE_OUTPUT &&
120 fulldecl->Semantic.SemanticName == TGSI_SEMANTIC_POSITION) {
121 info->writes_z = TRUE;
122 }
123 }
124 }
125 break;
126
127 case TGSI_TOKEN_TYPE_IMMEDIATE:
128 info->immediate_count++;
129 break;
130
131 default:
132 assert( 0 );
133 }
134 }
135
136 assert( info->file_max[TGSI_FILE_INPUT] + 1 == info->num_inputs );
137 assert( info->file_max[TGSI_FILE_OUTPUT] + 1 == info->num_outputs );
138
139 info->uses_kill = (info->opcode_count[TGSI_OPCODE_KIL] ||
140 info->opcode_count[TGSI_OPCODE_KILP]);
141
142 tgsi_parse_free (&parse);
143 }
144
145
146
147 /**
148 * Check if the given shader is a "passthrough" shader consisting of only
149 * MOV instructions of the form: MOV OUT[n], IN[n]
150 *
151 */
152 boolean
153 tgsi_is_passthrough_shader(const struct tgsi_token *tokens)
154 {
155 struct tgsi_parse_context parse;
156
157 /**
158 ** Setup to begin parsing input shader
159 **/
160 if (tgsi_parse_init(&parse, tokens) != TGSI_PARSE_OK) {
161 debug_printf("tgsi_parse_init() failed in tgsi_is_passthrough_shader()!\n");
162 return FALSE;
163 }
164
165 /**
166 ** Loop over incoming program tokens/instructions
167 */
168 while (!tgsi_parse_end_of_tokens(&parse)) {
169
170 tgsi_parse_token(&parse);
171
172 switch (parse.FullToken.Token.Type) {
173 case TGSI_TOKEN_TYPE_INSTRUCTION:
174 {
175 struct tgsi_full_instruction *fullinst =
176 &parse.FullToken.FullInstruction;
177 const struct tgsi_full_src_register *src =
178 &fullinst->FullSrcRegisters[0];
179 const struct tgsi_full_dst_register *dst =
180 &fullinst->FullDstRegisters[0];
181
182 /* Do a whole bunch of checks for a simple move */
183 if (fullinst->Instruction.Opcode != TGSI_OPCODE_MOV ||
184 src->SrcRegister.File != TGSI_FILE_INPUT ||
185 dst->DstRegister.File != TGSI_FILE_OUTPUT ||
186 src->SrcRegister.Index != dst->DstRegister.Index ||
187
188 src->SrcRegister.Negate ||
189 src->SrcRegisterExtMod.Negate ||
190 src->SrcRegisterExtMod.Absolute ||
191 src->SrcRegisterExtMod.Scale2X ||
192 src->SrcRegisterExtMod.Bias ||
193 src->SrcRegisterExtMod.Complement ||
194
195 src->SrcRegister.SwizzleX != TGSI_SWIZZLE_X ||
196 src->SrcRegister.SwizzleY != TGSI_SWIZZLE_Y ||
197 src->SrcRegister.SwizzleZ != TGSI_SWIZZLE_Z ||
198 src->SrcRegister.SwizzleW != TGSI_SWIZZLE_W ||
199
200 src->SrcRegisterExtSwz.ExtSwizzleX != TGSI_EXTSWIZZLE_X ||
201 src->SrcRegisterExtSwz.ExtSwizzleY != TGSI_EXTSWIZZLE_Y ||
202 src->SrcRegisterExtSwz.ExtSwizzleZ != TGSI_EXTSWIZZLE_Z ||
203 src->SrcRegisterExtSwz.ExtSwizzleW != TGSI_EXTSWIZZLE_W ||
204
205 dst->DstRegister.WriteMask != TGSI_WRITEMASK_XYZW)
206 {
207 tgsi_parse_free(&parse);
208 return FALSE;
209 }
210 }
211 break;
212
213 case TGSI_TOKEN_TYPE_DECLARATION:
214 /* fall-through */
215 case TGSI_TOKEN_TYPE_IMMEDIATE:
216 /* fall-through */
217 default:
218 ; /* no-op */
219 }
220 }
221
222 tgsi_parse_free(&parse);
223
224 /* if we get here, it's a pass-through shader */
225 return TRUE;
226 }