Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_dump_c.c
1 /**************************************************************************
2 *
3 * Copyright 2007-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 #include "util/u_debug.h"
29 #include "util/u_string.h"
30 #include "tgsi_dump_c.h"
31 #include "tgsi_build.h"
32 #include "tgsi_info.h"
33 #include "tgsi_parse.h"
34
35 static void
36 dump_enum(
37 const unsigned e,
38 const char **enums,
39 const unsigned enums_count )
40 {
41 if (e >= enums_count) {
42 debug_printf( "%u", e );
43 }
44 else {
45 debug_printf( "%s", enums[e] );
46 }
47 }
48
49 #define EOL() debug_printf( "\n" )
50 #define TXT(S) debug_printf( "%s", S )
51 #define CHR(C) debug_printf( "%c", C )
52 #define UIX(I) debug_printf( "0x%x", I )
53 #define UID(I) debug_printf( "%u", I )
54 #define SID(I) debug_printf( "%d", I )
55 #define FLT(F) debug_printf( "%10.4f", F )
56 #define ENM(E,ENUMS) dump_enum( E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
57
58 static const char *TGSI_PROCESSOR_TYPES[] =
59 {
60 "PROCESSOR_FRAGMENT",
61 "PROCESSOR_VERTEX",
62 "PROCESSOR_GEOMETRY"
63 };
64
65 static const char *TGSI_TOKEN_TYPES[] =
66 {
67 "TOKEN_TYPE_DECLARATION",
68 "TOKEN_TYPE_IMMEDIATE",
69 "TOKEN_TYPE_INSTRUCTION"
70 };
71
72 static const char *TGSI_FILES[TGSI_FILE_COUNT] =
73 {
74 "FILE_NULL",
75 "FILE_CONSTANT",
76 "FILE_INPUT",
77 "FILE_OUTPUT",
78 "FILE_TEMPORARY",
79 "FILE_SAMPLER",
80 "FILE_ADDRESS",
81 "FILE_IMMEDIATE",
82 "FILE_LOOP",
83 "FILE_PREDICATE"
84 };
85
86 static const char *TGSI_INTERPOLATES[] =
87 {
88 "INTERPOLATE_CONSTANT",
89 "INTERPOLATE_LINEAR",
90 "INTERPOLATE_PERSPECTIVE"
91 };
92
93 static const char *TGSI_SEMANTICS[] =
94 {
95 "SEMANTIC_POSITION",
96 "SEMANTIC_COLOR",
97 "SEMANTIC_BCOLOR",
98 "SEMANTIC_FOG",
99 "SEMANTIC_PSIZE",
100 "SEMANTIC_GENERIC",
101 "SEMANTIC_NORMAL"
102 };
103
104 static const char *TGSI_IMMS[] =
105 {
106 "IMM_FLOAT32"
107 };
108
109 static const char *TGSI_SATS[] =
110 {
111 "SAT_NONE",
112 "SAT_ZERO_ONE",
113 "SAT_MINUS_PLUS_ONE"
114 };
115
116 static const char *TGSI_SWIZZLES[] =
117 {
118 "SWIZZLE_X",
119 "SWIZZLE_Y",
120 "SWIZZLE_Z",
121 "SWIZZLE_W"
122 };
123
124 static const char *TGSI_TEXTURES[] =
125 {
126 "TEXTURE_UNKNOWN",
127 "TEXTURE_1D",
128 "TEXTURE_2D",
129 "TEXTURE_3D",
130 "TEXTURE_CUBE",
131 "TEXTURE_RECT",
132 "TEXTURE_SHADOW1D",
133 "TEXTURE_SHADOW2D",
134 "TEXTURE_SHADOWRECT"
135 };
136
137 static const char *TGSI_WRITEMASKS[] =
138 {
139 "0",
140 "WRITEMASK_X",
141 "WRITEMASK_Y",
142 "WRITEMASK_XY",
143 "WRITEMASK_Z",
144 "WRITEMASK_XZ",
145 "WRITEMASK_YZ",
146 "WRITEMASK_XYZ",
147 "WRITEMASK_W",
148 "WRITEMASK_XW",
149 "WRITEMASK_YW",
150 "WRITEMASK_XYW",
151 "WRITEMASK_ZW",
152 "WRITEMASK_XZW",
153 "WRITEMASK_YZW",
154 "WRITEMASK_XYZW"
155 };
156
157 static void
158 dump_declaration_verbose(
159 struct tgsi_full_declaration *decl,
160 unsigned ignored,
161 unsigned deflt,
162 struct tgsi_full_declaration *fd )
163 {
164 TXT( "\nFile : " );
165 ENM( decl->Declaration.File, TGSI_FILES );
166 if( deflt || fd->Declaration.UsageMask != decl->Declaration.UsageMask ) {
167 TXT( "\nUsageMask : " );
168 if( decl->Declaration.UsageMask & TGSI_WRITEMASK_X ) {
169 CHR( 'X' );
170 }
171 if( decl->Declaration.UsageMask & TGSI_WRITEMASK_Y ) {
172 CHR( 'Y' );
173 }
174 if( decl->Declaration.UsageMask & TGSI_WRITEMASK_Z ) {
175 CHR( 'Z' );
176 }
177 if( decl->Declaration.UsageMask & TGSI_WRITEMASK_W ) {
178 CHR( 'W' );
179 }
180 }
181 if( deflt || fd->Declaration.Interpolate != decl->Declaration.Interpolate ) {
182 TXT( "\nInterpolate: " );
183 ENM( decl->Declaration.Interpolate, TGSI_INTERPOLATES );
184 }
185 if( deflt || fd->Declaration.Semantic != decl->Declaration.Semantic ) {
186 TXT( "\nSemantic : " );
187 UID( decl->Declaration.Semantic );
188 }
189 if (deflt || fd->Declaration.Centroid != decl->Declaration.Centroid) {
190 TXT("\nCentroid : ");
191 UID(decl->Declaration.Centroid);
192 }
193 if (deflt || fd->Declaration.Invariant != decl->Declaration.Invariant) {
194 TXT("\nInvariant : ");
195 UID(decl->Declaration.Invariant);
196 }
197 if( ignored ) {
198 TXT( "\nPadding : " );
199 UIX( decl->Declaration.Padding );
200 }
201
202 EOL();
203 TXT( "\nFirst: " );
204 UID( decl->Range.First );
205 TXT( "\nLast : " );
206 UID( decl->Range.Last );
207
208 if( decl->Declaration.Semantic ) {
209 EOL();
210 TXT( "\nName : " );
211 ENM( decl->Semantic.Name, TGSI_SEMANTICS );
212 TXT( "\nIndex: " );
213 UID( decl->Semantic.Index );
214 if( ignored ) {
215 TXT( "\nPadding : " );
216 UIX( decl->Semantic.Padding );
217 }
218 }
219 }
220
221 static void
222 dump_immediate_verbose(
223 struct tgsi_full_immediate *imm,
224 unsigned ignored )
225 {
226 unsigned i;
227
228 TXT( "\nDataType : " );
229 ENM( imm->Immediate.DataType, TGSI_IMMS );
230 if( ignored ) {
231 TXT( "\nPadding : " );
232 UIX( imm->Immediate.Padding );
233 }
234
235 assert( imm->Immediate.NrTokens <= 4 + 1 );
236 for( i = 0; i < imm->Immediate.NrTokens - 1; i++ ) {
237 EOL();
238 switch( imm->Immediate.DataType ) {
239 case TGSI_IMM_FLOAT32:
240 TXT( "\nFloat: " );
241 FLT( imm->u[i].Float );
242 break;
243
244 default:
245 assert( 0 );
246 }
247 }
248 }
249
250 static void
251 dump_instruction_verbose(
252 struct tgsi_full_instruction *inst,
253 unsigned ignored,
254 unsigned deflt,
255 struct tgsi_full_instruction *fi )
256 {
257 unsigned i;
258
259 TXT( "\nOpcode : OPCODE_" );
260 TXT( tgsi_get_opcode_info( inst->Instruction.Opcode )->mnemonic );
261 if( deflt || fi->Instruction.Saturate != inst->Instruction.Saturate ) {
262 TXT( "\nSaturate : " );
263 ENM( inst->Instruction.Saturate, TGSI_SATS );
264 }
265 if( deflt || fi->Instruction.NumDstRegs != inst->Instruction.NumDstRegs ) {
266 TXT( "\nNumDstRegs : " );
267 UID( inst->Instruction.NumDstRegs );
268 }
269 if( deflt || fi->Instruction.NumSrcRegs != inst->Instruction.NumSrcRegs ) {
270 TXT( "\nNumSrcRegs : " );
271 UID( inst->Instruction.NumSrcRegs );
272 }
273 if (deflt || fi->Instruction.Predicate != inst->Instruction.Predicate) {
274 TXT("\nPredicate : ");
275 UID(inst->Instruction.Predicate);
276 }
277 if (deflt || fi->Instruction.Label != inst->Instruction.Label) {
278 TXT("\nLabel : ");
279 UID(inst->Instruction.Label);
280 }
281 if (deflt || fi->Instruction.Texture != inst->Instruction.Texture) {
282 TXT("\nTexture : ");
283 UID(inst->Instruction.Texture);
284 }
285 if( ignored ) {
286 TXT( "\nPadding : " );
287 UIX( inst->Instruction.Padding );
288 }
289
290 if (deflt || inst->Instruction.Label) {
291 EOL();
292 if (deflt || fi->Label.Label != inst->Label.Label) {
293 TXT( "\nLabel : " );
294 UID(inst->Label.Label);
295 }
296 if( ignored ) {
297 TXT( "\nPadding : " );
298 UIX(inst->Label.Padding);
299 }
300 }
301
302 if (deflt || inst->Instruction.Texture) {
303 EOL();
304 if (deflt || fi->Texture.Texture != inst->Texture.Texture) {
305 TXT( "\nTexture : " );
306 ENM(inst->Texture.Texture, TGSI_TEXTURES);
307 }
308 if( ignored ) {
309 TXT( "\nPadding : " );
310 UIX(inst->Texture.Padding);
311 }
312 }
313
314 for( i = 0; i < inst->Instruction.NumDstRegs; i++ ) {
315 struct tgsi_full_dst_register *dst = &inst->Dst[i];
316 struct tgsi_full_dst_register *fd = &fi->Dst[i];
317
318 EOL();
319 TXT( "\nFile : " );
320 ENM( dst->Register.File, TGSI_FILES );
321 if( deflt || fd->Register.WriteMask != dst->Register.WriteMask ) {
322 TXT( "\nWriteMask: " );
323 ENM( dst->Register.WriteMask, TGSI_WRITEMASKS );
324 }
325 if( ignored ) {
326 if( deflt || fd->Register.Indirect != dst->Register.Indirect ) {
327 TXT( "\nIndirect : " );
328 UID( dst->Register.Indirect );
329 }
330 if( deflt || fd->Register.Dimension != dst->Register.Dimension ) {
331 TXT( "\nDimension: " );
332 UID( dst->Register.Dimension );
333 }
334 }
335 if( deflt || fd->Register.Index != dst->Register.Index ) {
336 TXT( "\nIndex : " );
337 SID( dst->Register.Index );
338 }
339 if( ignored ) {
340 TXT( "\nPadding : " );
341 UIX( dst->Register.Padding );
342 }
343 }
344
345 for( i = 0; i < inst->Instruction.NumSrcRegs; i++ ) {
346 struct tgsi_full_src_register *src = &inst->Src[i];
347 struct tgsi_full_src_register *fs = &fi->Src[i];
348
349 EOL();
350 TXT( "\nFile : ");
351 ENM( src->Register.File, TGSI_FILES );
352 if( deflt || fs->Register.SwizzleX != src->Register.SwizzleX ) {
353 TXT( "\nSwizzleX : " );
354 ENM( src->Register.SwizzleX, TGSI_SWIZZLES );
355 }
356 if( deflt || fs->Register.SwizzleY != src->Register.SwizzleY ) {
357 TXT( "\nSwizzleY : " );
358 ENM( src->Register.SwizzleY, TGSI_SWIZZLES );
359 }
360 if( deflt || fs->Register.SwizzleZ != src->Register.SwizzleZ ) {
361 TXT( "\nSwizzleZ : " );
362 ENM( src->Register.SwizzleZ, TGSI_SWIZZLES );
363 }
364 if( deflt || fs->Register.SwizzleW != src->Register.SwizzleW ) {
365 TXT( "\nSwizzleW : " );
366 ENM( src->Register.SwizzleW, TGSI_SWIZZLES );
367 }
368 if (deflt || fs->Register.Absolute != src->Register.Absolute) {
369 TXT("\nAbsolute : ");
370 UID(src->Register.Absolute);
371 }
372 if( deflt || fs->Register.Negate != src->Register.Negate ) {
373 TXT( "\nNegate : " );
374 UID( src->Register.Negate );
375 }
376 if( ignored ) {
377 if( deflt || fs->Register.Indirect != src->Register.Indirect ) {
378 TXT( "\nIndirect : " );
379 UID( src->Register.Indirect );
380 }
381 if( deflt || fs->Register.Dimension != src->Register.Dimension ) {
382 TXT( "\nDimension: " );
383 UID( src->Register.Dimension );
384 }
385 }
386 if( deflt || fs->Register.Index != src->Register.Index ) {
387 TXT( "\nIndex : " );
388 SID( src->Register.Index );
389 }
390 }
391 }
392
393 void
394 tgsi_dump_c(
395 const struct tgsi_token *tokens,
396 uint flags )
397 {
398 struct tgsi_parse_context parse;
399 struct tgsi_full_instruction fi;
400 struct tgsi_full_declaration fd;
401 uint ignored = flags & TGSI_DUMP_C_IGNORED;
402 uint deflt = flags & TGSI_DUMP_C_DEFAULT;
403
404 tgsi_parse_init( &parse, tokens );
405
406 TXT( "tgsi-dump begin -----------------" );
407
408 TXT( "\nHeaderSize: " );
409 UID( parse.FullHeader.Header.HeaderSize );
410 TXT( "\nBodySize : " );
411 UID( parse.FullHeader.Header.BodySize );
412 TXT( "\nProcessor : " );
413 ENM( parse.FullHeader.Processor.Processor, TGSI_PROCESSOR_TYPES );
414 EOL();
415
416 fi = tgsi_default_full_instruction();
417 fd = tgsi_default_full_declaration();
418
419 while( !tgsi_parse_end_of_tokens( &parse ) ) {
420 tgsi_parse_token( &parse );
421
422 TXT( "\nType : " );
423 ENM( parse.FullToken.Token.Type, TGSI_TOKEN_TYPES );
424 if( ignored ) {
425 TXT( "\nSize : " );
426 UID( parse.FullToken.Token.NrTokens );
427 }
428
429 switch( parse.FullToken.Token.Type ) {
430 case TGSI_TOKEN_TYPE_DECLARATION:
431 dump_declaration_verbose(
432 &parse.FullToken.FullDeclaration,
433 ignored,
434 deflt,
435 &fd );
436 break;
437
438 case TGSI_TOKEN_TYPE_IMMEDIATE:
439 dump_immediate_verbose(
440 &parse.FullToken.FullImmediate,
441 ignored );
442 break;
443
444 case TGSI_TOKEN_TYPE_INSTRUCTION:
445 dump_instruction_verbose(
446 &parse.FullToken.FullInstruction,
447 ignored,
448 deflt,
449 &fi );
450 break;
451
452 default:
453 assert( 0 );
454 }
455
456 EOL();
457 }
458
459 TXT( "\ntgsi-dump end -------------------\n" );
460
461 tgsi_parse_free( &parse );
462 }