Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_dump.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 "util/u_math.h"
31 #include "util/u_memory.h"
32 #include "tgsi_dump.h"
33 #include "tgsi_info.h"
34 #include "tgsi_iterate.h"
35
36
37 /** Number of spaces to indent for IF/LOOP/etc */
38 static const int indent_spaces = 3;
39
40
41 struct dump_ctx
42 {
43 struct tgsi_iterate_context iter;
44
45 uint instno;
46 int indent;
47
48 uint indentation;
49
50 void (*printf)(struct dump_ctx *ctx, const char *format, ...);
51 };
52
53 static void
54 dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
55 {
56 va_list ap;
57 (void)ctx;
58 va_start(ap, format);
59 debug_vprintf(format, ap);
60 va_end(ap);
61 }
62
63 static void
64 dump_enum(
65 struct dump_ctx *ctx,
66 uint e,
67 const char **enums,
68 uint enum_count )
69 {
70 if (e >= enum_count)
71 ctx->printf( ctx, "%u", e );
72 else
73 ctx->printf( ctx, "%s", enums[e] );
74 }
75
76 #define EOL() ctx->printf( ctx, "\n" )
77 #define TXT(S) ctx->printf( ctx, "%s", S )
78 #define CHR(C) ctx->printf( ctx, "%c", C )
79 #define UIX(I) ctx->printf( ctx, "0x%x", I )
80 #define UID(I) ctx->printf( ctx, "%u", I )
81 #define INSTID(I) ctx->printf( ctx, "% 3u", I )
82 #define SID(I) ctx->printf( ctx, "%d", I )
83 #define FLT(F) ctx->printf( ctx, "%10.4f", F )
84 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
85
86 static const char *processor_type_names[] =
87 {
88 "FRAG",
89 "VERT",
90 "GEOM"
91 };
92
93 static const char *file_names[TGSI_FILE_COUNT] =
94 {
95 "NULL",
96 "CONST",
97 "IN",
98 "OUT",
99 "TEMP",
100 "SAMP",
101 "ADDR",
102 "IMM",
103 "LOOP"
104 };
105
106 static const char *interpolate_names[] =
107 {
108 "CONSTANT",
109 "LINEAR",
110 "PERSPECTIVE"
111 };
112
113 static const char *semantic_names[] =
114 {
115 "POSITION",
116 "COLOR",
117 "BCOLOR",
118 "FOG",
119 "PSIZE",
120 "GENERIC",
121 "NORMAL",
122 "FACE"
123 };
124
125 static const char *immediate_type_names[] =
126 {
127 "FLT32"
128 };
129
130 static const char *swizzle_names[] =
131 {
132 "x",
133 "y",
134 "z",
135 "w"
136 };
137
138 static const char *texture_names[] =
139 {
140 "UNKNOWN",
141 "1D",
142 "2D",
143 "3D",
144 "CUBE",
145 "RECT",
146 "SHADOW1D",
147 "SHADOW2D",
148 "SHADOWRECT"
149 };
150
151 static const char *extswizzle_names[] =
152 {
153 "x",
154 "y",
155 "z",
156 "w",
157 "0",
158 "1"
159 };
160
161 static const char *modulate_names[TGSI_MODULATE_COUNT] =
162 {
163 "",
164 "_2X",
165 "_4X",
166 "_8X",
167 "_D2",
168 "_D4",
169 "_D8"
170 };
171
172 static void
173 _dump_register(
174 struct dump_ctx *ctx,
175 uint file,
176 int first,
177 int last )
178 {
179 ENM( file, file_names );
180 CHR( '[' );
181 SID( first );
182 if (first != last) {
183 TXT( ".." );
184 SID( last );
185 }
186 CHR( ']' );
187 }
188
189 static void
190 _dump_register_ind(
191 struct dump_ctx *ctx,
192 uint file,
193 int index,
194 uint ind_file,
195 int ind_index,
196 uint ind_swizzle )
197 {
198 ENM( file, file_names );
199 CHR( '[' );
200 ENM( ind_file, file_names );
201 CHR( '[' );
202 SID( ind_index );
203 TXT( "]." );
204 ENM( ind_swizzle, swizzle_names );
205 if (index != 0) {
206 if (index > 0)
207 CHR( '+' );
208 SID( index );
209 }
210 CHR( ']' );
211 }
212
213 static void
214 _dump_writemask(
215 struct dump_ctx *ctx,
216 uint writemask )
217 {
218 if (writemask != TGSI_WRITEMASK_XYZW) {
219 CHR( '.' );
220 if (writemask & TGSI_WRITEMASK_X)
221 CHR( 'x' );
222 if (writemask & TGSI_WRITEMASK_Y)
223 CHR( 'y' );
224 if (writemask & TGSI_WRITEMASK_Z)
225 CHR( 'z' );
226 if (writemask & TGSI_WRITEMASK_W)
227 CHR( 'w' );
228 }
229 }
230
231 static boolean
232 iter_declaration(
233 struct tgsi_iterate_context *iter,
234 struct tgsi_full_declaration *decl )
235 {
236 struct dump_ctx *ctx = (struct dump_ctx *)iter;
237
238 assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT);
239 assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT);
240
241 TXT( "DCL " );
242
243 _dump_register(
244 ctx,
245 decl->Declaration.File,
246 decl->DeclarationRange.First,
247 decl->DeclarationRange.Last );
248 _dump_writemask(
249 ctx,
250 decl->Declaration.UsageMask );
251
252 if (decl->Declaration.Semantic) {
253 TXT( ", " );
254 ENM( decl->Semantic.SemanticName, semantic_names );
255 if (decl->Semantic.SemanticIndex != 0 ||
256 decl->Semantic.SemanticName == TGSI_SEMANTIC_GENERIC) {
257 CHR( '[' );
258 UID( decl->Semantic.SemanticIndex );
259 CHR( ']' );
260 }
261 }
262
263 if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT &&
264 decl->Declaration.File == TGSI_FILE_INPUT)
265 {
266 TXT( ", " );
267 ENM( decl->Declaration.Interpolate, interpolate_names );
268 }
269
270 if (decl->Declaration.Centroid) {
271 TXT( ", CENTROID" );
272 }
273
274 if (decl->Declaration.Invariant) {
275 TXT( ", INVARIANT" );
276 }
277
278 EOL();
279
280 return TRUE;
281 }
282
283 void
284 tgsi_dump_declaration(
285 const struct tgsi_full_declaration *decl )
286 {
287 struct dump_ctx ctx;
288
289 ctx.printf = dump_ctx_printf;
290
291 iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl );
292 }
293
294 static boolean
295 iter_immediate(
296 struct tgsi_iterate_context *iter,
297 struct tgsi_full_immediate *imm )
298 {
299 struct dump_ctx *ctx = (struct dump_ctx *) iter;
300
301 uint i;
302
303 TXT( "IMM " );
304 ENM( imm->Immediate.DataType, immediate_type_names );
305
306 TXT( " { " );
307
308 assert( imm->Immediate.NrTokens <= 4 + 1 );
309 for (i = 0; i < imm->Immediate.NrTokens - 1; i++) {
310 switch (imm->Immediate.DataType) {
311 case TGSI_IMM_FLOAT32:
312 FLT( imm->u[i].Float );
313 break;
314 default:
315 assert( 0 );
316 }
317
318 if (i < imm->Immediate.NrTokens - 2)
319 TXT( ", " );
320 }
321 TXT( " }" );
322
323 EOL();
324
325 return TRUE;
326 }
327
328 void
329 tgsi_dump_immediate(
330 const struct tgsi_full_immediate *imm )
331 {
332 struct dump_ctx ctx;
333
334 ctx.printf = dump_ctx_printf;
335
336 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm );
337 }
338
339 static boolean
340 iter_instruction(
341 struct tgsi_iterate_context *iter,
342 struct tgsi_full_instruction *inst )
343 {
344 struct dump_ctx *ctx = (struct dump_ctx *) iter;
345 uint instno = ctx->instno++;
346 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode );
347 uint i;
348 boolean first_reg = TRUE;
349
350 INSTID( instno );
351 TXT( ": " );
352
353 ctx->indent -= info->pre_dedent;
354 for(i = 0; (int)i < ctx->indent; ++i)
355 TXT( " " );
356 ctx->indent += info->post_indent;
357
358 TXT( info->mnemonic );
359
360 switch (inst->Instruction.Saturate) {
361 case TGSI_SAT_NONE:
362 break;
363 case TGSI_SAT_ZERO_ONE:
364 TXT( "_SAT" );
365 break;
366 case TGSI_SAT_MINUS_PLUS_ONE:
367 TXT( "_SATNV" );
368 break;
369 default:
370 assert( 0 );
371 }
372
373 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
374 const struct tgsi_full_dst_register *dst = &inst->FullDstRegisters[i];
375
376 if (!first_reg)
377 CHR( ',' );
378 CHR( ' ' );
379
380 if (dst->DstRegister.Indirect) {
381 _dump_register_ind(
382 ctx,
383 dst->DstRegister.File,
384 dst->DstRegister.Index,
385 dst->DstRegisterInd.File,
386 dst->DstRegisterInd.Index,
387 dst->DstRegisterInd.SwizzleX );
388 }
389 else {
390 _dump_register(
391 ctx,
392 dst->DstRegister.File,
393 dst->DstRegister.Index,
394 dst->DstRegister.Index );
395 }
396 ENM( dst->DstRegisterExtModulate.Modulate, modulate_names );
397 _dump_writemask( ctx, dst->DstRegister.WriteMask );
398
399 first_reg = FALSE;
400 }
401
402 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
403 const struct tgsi_full_src_register *src = &inst->FullSrcRegisters[i];
404
405 if (!first_reg)
406 CHR( ',' );
407 CHR( ' ' );
408
409 if (src->SrcRegisterExtMod.Negate)
410 TXT( "-(" );
411 if (src->SrcRegisterExtMod.Absolute)
412 CHR( '|' );
413 if (src->SrcRegisterExtMod.Scale2X)
414 TXT( "2*(" );
415 if (src->SrcRegisterExtMod.Bias)
416 CHR( '(' );
417 if (src->SrcRegisterExtMod.Complement)
418 TXT( "1-(" );
419 if (src->SrcRegister.Negate)
420 CHR( '-' );
421
422 if (src->SrcRegister.Indirect) {
423 _dump_register_ind(
424 ctx,
425 src->SrcRegister.File,
426 src->SrcRegister.Index,
427 src->SrcRegisterInd.File,
428 src->SrcRegisterInd.Index,
429 src->SrcRegisterInd.SwizzleX );
430 }
431 else {
432 _dump_register(
433 ctx,
434 src->SrcRegister.File,
435 src->SrcRegister.Index,
436 src->SrcRegister.Index );
437 }
438
439 if (src->SrcRegister.SwizzleX != TGSI_SWIZZLE_X ||
440 src->SrcRegister.SwizzleY != TGSI_SWIZZLE_Y ||
441 src->SrcRegister.SwizzleZ != TGSI_SWIZZLE_Z ||
442 src->SrcRegister.SwizzleW != TGSI_SWIZZLE_W) {
443 CHR( '.' );
444 ENM( src->SrcRegister.SwizzleX, swizzle_names );
445 ENM( src->SrcRegister.SwizzleY, swizzle_names );
446 ENM( src->SrcRegister.SwizzleZ, swizzle_names );
447 ENM( src->SrcRegister.SwizzleW, swizzle_names );
448 }
449 if (src->SrcRegisterExtSwz.ExtSwizzleX != TGSI_EXTSWIZZLE_X ||
450 src->SrcRegisterExtSwz.ExtSwizzleY != TGSI_EXTSWIZZLE_Y ||
451 src->SrcRegisterExtSwz.ExtSwizzleZ != TGSI_EXTSWIZZLE_Z ||
452 src->SrcRegisterExtSwz.ExtSwizzleW != TGSI_EXTSWIZZLE_W) {
453 CHR( '.' );
454 if (src->SrcRegisterExtSwz.NegateX)
455 TXT("-");
456 ENM( src->SrcRegisterExtSwz.ExtSwizzleX, extswizzle_names );
457 if (src->SrcRegisterExtSwz.NegateY)
458 TXT("-");
459 ENM( src->SrcRegisterExtSwz.ExtSwizzleY, extswizzle_names );
460 if (src->SrcRegisterExtSwz.NegateZ)
461 TXT("-");
462 ENM( src->SrcRegisterExtSwz.ExtSwizzleZ, extswizzle_names );
463 if (src->SrcRegisterExtSwz.NegateW)
464 TXT("-");
465 ENM( src->SrcRegisterExtSwz.ExtSwizzleW, extswizzle_names );
466 }
467
468 if (src->SrcRegisterExtMod.Complement)
469 CHR( ')' );
470 if (src->SrcRegisterExtMod.Bias)
471 TXT( ")-.5" );
472 if (src->SrcRegisterExtMod.Scale2X)
473 CHR( ')' );
474 if (src->SrcRegisterExtMod.Absolute)
475 CHR( '|' );
476 if (src->SrcRegisterExtMod.Negate)
477 CHR( ')' );
478
479 first_reg = FALSE;
480 }
481
482 if (inst->InstructionExtTexture.Texture != TGSI_TEXTURE_UNKNOWN) {
483 TXT( ", " );
484 ENM( inst->InstructionExtTexture.Texture, texture_names );
485 }
486
487 switch (inst->Instruction.Opcode) {
488 case TGSI_OPCODE_IF:
489 case TGSI_OPCODE_ELSE:
490 case TGSI_OPCODE_BGNLOOP:
491 case TGSI_OPCODE_ENDLOOP:
492 case TGSI_OPCODE_CAL:
493 TXT( " :" );
494 UID( inst->InstructionExtLabel.Label );
495 break;
496 }
497
498 /* update indentation */
499 if (inst->Instruction.Opcode == TGSI_OPCODE_IF ||
500 inst->Instruction.Opcode == TGSI_OPCODE_ELSE ||
501 inst->Instruction.Opcode == TGSI_OPCODE_BGNFOR ||
502 inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) {
503 ctx->indentation += indent_spaces;
504 }
505
506 EOL();
507
508 return TRUE;
509 }
510
511 void
512 tgsi_dump_instruction(
513 const struct tgsi_full_instruction *inst,
514 uint instno )
515 {
516 struct dump_ctx ctx;
517
518 ctx.instno = instno;
519 ctx.indent = 0;
520 ctx.printf = dump_ctx_printf;
521 ctx.indentation = 0;
522
523 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst );
524 }
525
526 static boolean
527 prolog(
528 struct tgsi_iterate_context *iter )
529 {
530 struct dump_ctx *ctx = (struct dump_ctx *) iter;
531 ENM( iter->processor.Processor, processor_type_names );
532 UID( iter->version.MajorVersion );
533 CHR( '.' );
534 UID( iter->version.MinorVersion );
535 EOL();
536 return TRUE;
537 }
538
539 void
540 tgsi_dump(
541 const struct tgsi_token *tokens,
542 uint flags )
543 {
544 struct dump_ctx ctx;
545
546 ctx.iter.prolog = prolog;
547 ctx.iter.iterate_instruction = iter_instruction;
548 ctx.iter.iterate_declaration = iter_declaration;
549 ctx.iter.iterate_immediate = iter_immediate;
550 ctx.iter.epilog = NULL;
551
552 ctx.instno = 0;
553 ctx.indent = 0;
554 ctx.printf = dump_ctx_printf;
555 ctx.indentation = 0;
556
557 tgsi_iterate_shader( tokens, &ctx.iter );
558 }
559
560 struct str_dump_ctx
561 {
562 struct dump_ctx base;
563 char *str;
564 char *ptr;
565 int left;
566 };
567
568 static void
569 str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
570 {
571 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx;
572
573 if(sctx->left > 1) {
574 int written;
575 va_list ap;
576 va_start(ap, format);
577 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap);
578 va_end(ap);
579
580 /* Some complicated logic needed to handle the return value of
581 * vsnprintf:
582 */
583 if (written > 0) {
584 written = MIN2(sctx->left, written);
585 sctx->ptr += written;
586 sctx->left -= written;
587 }
588 }
589 }
590
591 void
592 tgsi_dump_str(
593 const struct tgsi_token *tokens,
594 uint flags,
595 char *str,
596 size_t size)
597 {
598 struct str_dump_ctx ctx;
599
600 ctx.base.iter.prolog = prolog;
601 ctx.base.iter.iterate_instruction = iter_instruction;
602 ctx.base.iter.iterate_declaration = iter_declaration;
603 ctx.base.iter.iterate_immediate = iter_immediate;
604 ctx.base.iter.epilog = NULL;
605
606 ctx.base.instno = 0;
607 ctx.base.indent = 0;
608 ctx.base.printf = &str_dump_ctx_printf;
609 ctx.base.indentation = 0;
610
611 ctx.str = str;
612 ctx.str[0] = 0;
613 ctx.ptr = str;
614 ctx.left = (int)size;
615
616 tgsi_iterate_shader( tokens, &ctx.base.iter );
617 }