tgsi_dump.c revision 2b221e11da7a8bf759e3c359f22ba6f49d5f0997
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 */ 38static const int indent_spaces = 3; 39 40 41struct 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 53static void 54dump_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 63static void 64dump_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 86static const char *processor_type_names[] = 87{ 88 "FRAG", 89 "VERT", 90 "GEOM" 91}; 92 93static 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 "PRED", 104 "SV", 105 "IMMX" 106}; 107 108static const char *interpolate_names[] = 109{ 110 "CONSTANT", 111 "LINEAR", 112 "PERSPECTIVE" 113}; 114 115static const char *semantic_names[] = 116{ 117 "POSITION", 118 "COLOR", 119 "BCOLOR", 120 "FOG", 121 "PSIZE", 122 "GENERIC", 123 "NORMAL", 124 "FACE", 125 "EDGEFLAG", 126 "PRIM_ID", 127 "INSTANCEID" 128}; 129 130static const char *immediate_type_names[] = 131{ 132 "FLT32", 133 "UINT32", 134 "INT32" 135}; 136 137static const char *swizzle_names[] = 138{ 139 "x", 140 "y", 141 "z", 142 "w" 143}; 144 145static const char *texture_names[] = 146{ 147 "UNKNOWN", 148 "1D", 149 "2D", 150 "3D", 151 "CUBE", 152 "RECT", 153 "SHADOW1D", 154 "SHADOW2D", 155 "SHADOWRECT" 156}; 157 158static const char *property_names[] = 159{ 160 "GS_INPUT_PRIMITIVE", 161 "GS_OUTPUT_PRIMITIVE", 162 "GS_MAX_OUTPUT_VERTICES", 163 "FS_COORD_ORIGIN", 164 "FS_COORD_PIXEL_CENTER" 165}; 166 167static const char *primitive_names[] = 168{ 169 "POINTS", 170 "LINES", 171 "LINE_LOOP", 172 "LINE_STRIP", 173 "TRIANGLES", 174 "TRIANGLE_STRIP", 175 "TRIANGLE_FAN", 176 "QUADS", 177 "QUAD_STRIP", 178 "POLYGON" 179}; 180 181static const char *fs_coord_origin_names[] = 182{ 183 "UPPER_LEFT", 184 "LOWER_LEFT" 185}; 186 187static const char *fs_coord_pixel_center_names[] = 188{ 189 "HALF_INTEGER", 190 "INTEGER" 191}; 192 193 194static void 195_dump_register_dst( 196 struct dump_ctx *ctx, 197 uint file, 198 int index) 199{ 200 ENM( file, file_names ); 201 202 CHR( '[' ); 203 SID( index ); 204 CHR( ']' ); 205} 206 207 208static void 209_dump_register_src( 210 struct dump_ctx *ctx, 211 const struct tgsi_full_src_register *src ) 212{ 213 ENM(src->Register.File, file_names); 214 if (src->Register.Dimension) { 215 if (src->Dimension.Indirect) { 216 CHR( '[' ); 217 ENM( src->DimIndirect.File, file_names ); 218 CHR( '[' ); 219 SID( src->DimIndirect.Index ); 220 TXT( "]." ); 221 ENM( src->DimIndirect.SwizzleX, swizzle_names ); 222 if (src->Dimension.Index != 0) { 223 if (src->Dimension.Index > 0) 224 CHR( '+' ); 225 SID( src->Dimension.Index ); 226 } 227 CHR( ']' ); 228 } else { 229 CHR('['); 230 SID(src->Dimension.Index); 231 CHR(']'); 232 } 233 } 234 if (src->Register.Indirect) { 235 CHR( '[' ); 236 ENM( src->Indirect.File, file_names ); 237 CHR( '[' ); 238 SID( src->Indirect.Index ); 239 TXT( "]." ); 240 ENM( src->Indirect.SwizzleX, swizzle_names ); 241 if (src->Register.Index != 0) { 242 if (src->Register.Index > 0) 243 CHR( '+' ); 244 SID( src->Register.Index ); 245 } 246 CHR( ']' ); 247 } else { 248 CHR( '[' ); 249 SID( src->Register.Index ); 250 CHR( ']' ); 251 } 252} 253 254static void 255_dump_register_ind( 256 struct dump_ctx *ctx, 257 uint file, 258 int index, 259 uint ind_file, 260 int ind_index, 261 uint ind_swizzle ) 262{ 263 ENM( file, file_names ); 264 CHR( '[' ); 265 ENM( ind_file, file_names ); 266 CHR( '[' ); 267 SID( ind_index ); 268 TXT( "]." ); 269 ENM( ind_swizzle, swizzle_names ); 270 if (index != 0) { 271 if (index > 0) 272 CHR( '+' ); 273 SID( index ); 274 } 275 CHR( ']' ); 276} 277 278static void 279_dump_writemask( 280 struct dump_ctx *ctx, 281 uint writemask ) 282{ 283 if (writemask != TGSI_WRITEMASK_XYZW) { 284 CHR( '.' ); 285 if (writemask & TGSI_WRITEMASK_X) 286 CHR( 'x' ); 287 if (writemask & TGSI_WRITEMASK_Y) 288 CHR( 'y' ); 289 if (writemask & TGSI_WRITEMASK_Z) 290 CHR( 'z' ); 291 if (writemask & TGSI_WRITEMASK_W) 292 CHR( 'w' ); 293 } 294} 295 296static void 297dump_imm_data(struct tgsi_iterate_context *iter, 298 union tgsi_immediate_data *data, 299 unsigned num_tokens, 300 unsigned data_type) 301{ 302 struct dump_ctx *ctx = (struct dump_ctx *)iter; 303 unsigned i ; 304 305 TXT( " {" ); 306 307 assert( num_tokens <= 4 ); 308 for (i = 0; i < num_tokens; i++) { 309 switch (data_type) { 310 case TGSI_IMM_FLOAT32: 311 FLT( data[i].Float ); 312 break; 313 case TGSI_IMM_UINT32: 314 UID(data[i].Uint); 315 break; 316 case TGSI_IMM_INT32: 317 SID(data[i].Int); 318 break; 319 default: 320 assert( 0 ); 321 } 322 323 if (i < num_tokens - 1) 324 TXT( ", " ); 325 } 326 TXT( "}" ); 327} 328 329static boolean 330iter_declaration( 331 struct tgsi_iterate_context *iter, 332 struct tgsi_full_declaration *decl ) 333{ 334 struct dump_ctx *ctx = (struct dump_ctx *)iter; 335 336 assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT); 337 assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT); 338 339 TXT( "DCL " ); 340 341 ENM(decl->Declaration.File, file_names); 342 343 /* all geometry shader inputs are two dimensional */ 344 if (decl->Declaration.File == TGSI_FILE_INPUT && 345 iter->processor.Processor == TGSI_PROCESSOR_GEOMETRY) { 346 TXT("[]"); 347 } 348 349 if (decl->Declaration.Dimension) { 350 CHR('['); 351 SID(decl->Dim.Index2D); 352 CHR(']'); 353 } 354 355 CHR('['); 356 SID(decl->Range.First); 357 if (decl->Range.First != decl->Range.Last) { 358 TXT(".."); 359 SID(decl->Range.Last); 360 } 361 CHR(']'); 362 363 _dump_writemask( 364 ctx, 365 decl->Declaration.UsageMask ); 366 367 if (decl->Declaration.Semantic) { 368 TXT( ", " ); 369 ENM( decl->Semantic.Name, semantic_names ); 370 if (decl->Semantic.Index != 0 || 371 decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) { 372 CHR( '[' ); 373 UID( decl->Semantic.Index ); 374 CHR( ']' ); 375 } 376 } 377 378 if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT && 379 decl->Declaration.File == TGSI_FILE_INPUT) 380 { 381 TXT( ", " ); 382 ENM( decl->Declaration.Interpolate, interpolate_names ); 383 } 384 385 if (decl->Declaration.Centroid) { 386 TXT( ", CENTROID" ); 387 } 388 389 if (decl->Declaration.Invariant) { 390 TXT( ", INVARIANT" ); 391 } 392 393 if (decl->Declaration.CylindricalWrap) { 394 TXT(", CYLWRAP_"); 395 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_X) { 396 CHR('X'); 397 } 398 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Y) { 399 CHR('Y'); 400 } 401 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Z) { 402 CHR('Z'); 403 } 404 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_W) { 405 CHR('W'); 406 } 407 } 408 409 if (decl->Declaration.File == TGSI_FILE_IMMEDIATE_ARRAY) { 410 unsigned i; 411 char range_indent[4]; 412 413 TXT(" {"); 414 415 if (decl->Range.Last < 10) 416 range_indent[0] = '\0'; 417 else if (decl->Range.Last < 100) { 418 range_indent[0] = ' '; 419 range_indent[1] = '\0'; 420 } else if (decl->Range.Last < 1000) { 421 range_indent[0] = ' '; 422 range_indent[1] = ' '; 423 range_indent[2] = '\0'; 424 } else { 425 range_indent[0] = ' '; 426 range_indent[1] = ' '; 427 range_indent[2] = ' '; 428 range_indent[3] = '\0'; 429 } 430 431 dump_imm_data(iter, decl->ImmediateData.u, 432 4, TGSI_IMM_FLOAT32); 433 for(i = 1; i <= decl->Range.Last; ++i) { 434 /* indent by strlen of: 435 * "DCL IMMX[0..1] {" */ 436 CHR('\n'); 437 TXT( " " ); 438 TXT( range_indent ); 439 dump_imm_data(iter, decl->ImmediateData.u + i, 440 4, TGSI_IMM_FLOAT32); 441 } 442 443 TXT(" }"); 444 } 445 446 EOL(); 447 448 return TRUE; 449} 450 451void 452tgsi_dump_declaration( 453 const struct tgsi_full_declaration *decl ) 454{ 455 struct dump_ctx ctx; 456 457 ctx.printf = dump_ctx_printf; 458 459 iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl ); 460} 461 462static boolean 463iter_property( 464 struct tgsi_iterate_context *iter, 465 struct tgsi_full_property *prop ) 466{ 467 int i; 468 struct dump_ctx *ctx = (struct dump_ctx *)iter; 469 470 assert(Elements(property_names) == TGSI_PROPERTY_COUNT); 471 472 TXT( "PROPERTY " ); 473 ENM(prop->Property.PropertyName, property_names); 474 475 if (prop->Property.NrTokens > 1) 476 TXT(" "); 477 478 for (i = 0; i < prop->Property.NrTokens - 1; ++i) { 479 switch (prop->Property.PropertyName) { 480 case TGSI_PROPERTY_GS_INPUT_PRIM: 481 case TGSI_PROPERTY_GS_OUTPUT_PRIM: 482 ENM(prop->u[i].Data, primitive_names); 483 break; 484 case TGSI_PROPERTY_FS_COORD_ORIGIN: 485 ENM(prop->u[i].Data, fs_coord_origin_names); 486 break; 487 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER: 488 ENM(prop->u[i].Data, fs_coord_pixel_center_names); 489 break; 490 default: 491 SID( prop->u[i].Data ); 492 break; 493 } 494 if (i < prop->Property.NrTokens - 2) 495 TXT( ", " ); 496 } 497 EOL(); 498 499 return TRUE; 500} 501 502void tgsi_dump_property( 503 const struct tgsi_full_property *prop ) 504{ 505 struct dump_ctx ctx; 506 507 ctx.printf = dump_ctx_printf; 508 509 iter_property( &ctx.iter, (struct tgsi_full_property *)prop ); 510} 511 512static boolean 513iter_immediate( 514 struct tgsi_iterate_context *iter, 515 struct tgsi_full_immediate *imm ) 516{ 517 struct dump_ctx *ctx = (struct dump_ctx *) iter; 518 519 TXT( "IMM " ); 520 ENM( imm->Immediate.DataType, immediate_type_names ); 521 522 dump_imm_data(iter, imm->u, imm->Immediate.NrTokens - 1, 523 imm->Immediate.DataType); 524 525 EOL(); 526 527 return TRUE; 528} 529 530void 531tgsi_dump_immediate( 532 const struct tgsi_full_immediate *imm ) 533{ 534 struct dump_ctx ctx; 535 536 ctx.printf = dump_ctx_printf; 537 538 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm ); 539} 540 541static boolean 542iter_instruction( 543 struct tgsi_iterate_context *iter, 544 struct tgsi_full_instruction *inst ) 545{ 546 struct dump_ctx *ctx = (struct dump_ctx *) iter; 547 uint instno = ctx->instno++; 548 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode ); 549 uint i; 550 boolean first_reg = TRUE; 551 552 INSTID( instno ); 553 TXT( ": " ); 554 555 ctx->indent -= info->pre_dedent; 556 for(i = 0; (int)i < ctx->indent; ++i) 557 TXT( " " ); 558 ctx->indent += info->post_indent; 559 560 if (inst->Instruction.Predicate) { 561 CHR( '(' ); 562 563 if (inst->Predicate.Negate) 564 CHR( '!' ); 565 566 TXT( "PRED[" ); 567 SID( inst->Predicate.Index ); 568 CHR( ']' ); 569 570 if (inst->Predicate.SwizzleX != TGSI_SWIZZLE_X || 571 inst->Predicate.SwizzleY != TGSI_SWIZZLE_Y || 572 inst->Predicate.SwizzleZ != TGSI_SWIZZLE_Z || 573 inst->Predicate.SwizzleW != TGSI_SWIZZLE_W) { 574 CHR( '.' ); 575 ENM( inst->Predicate.SwizzleX, swizzle_names ); 576 ENM( inst->Predicate.SwizzleY, swizzle_names ); 577 ENM( inst->Predicate.SwizzleZ, swizzle_names ); 578 ENM( inst->Predicate.SwizzleW, swizzle_names ); 579 } 580 581 TXT( ") " ); 582 } 583 584 TXT( info->mnemonic ); 585 586 switch (inst->Instruction.Saturate) { 587 case TGSI_SAT_NONE: 588 break; 589 case TGSI_SAT_ZERO_ONE: 590 TXT( "_SAT" ); 591 break; 592 case TGSI_SAT_MINUS_PLUS_ONE: 593 TXT( "_SATNV" ); 594 break; 595 default: 596 assert( 0 ); 597 } 598 599 for (i = 0; i < inst->Instruction.NumDstRegs; i++) { 600 const struct tgsi_full_dst_register *dst = &inst->Dst[i]; 601 602 if (!first_reg) 603 CHR( ',' ); 604 CHR( ' ' ); 605 606 if (dst->Register.Indirect) { 607 _dump_register_ind( 608 ctx, 609 dst->Register.File, 610 dst->Register.Index, 611 dst->Indirect.File, 612 dst->Indirect.Index, 613 dst->Indirect.SwizzleX ); 614 } 615 else { 616 _dump_register_dst( 617 ctx, 618 dst->Register.File, 619 dst->Register.Index ); 620 } 621 _dump_writemask( ctx, dst->Register.WriteMask ); 622 623 first_reg = FALSE; 624 } 625 626 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) { 627 const struct tgsi_full_src_register *src = &inst->Src[i]; 628 629 if (!first_reg) 630 CHR( ',' ); 631 CHR( ' ' ); 632 633 if (src->Register.Negate) 634 CHR( '-' ); 635 if (src->Register.Absolute) 636 CHR( '|' ); 637 638 _dump_register_src(ctx, src); 639 640 if (src->Register.SwizzleX != TGSI_SWIZZLE_X || 641 src->Register.SwizzleY != TGSI_SWIZZLE_Y || 642 src->Register.SwizzleZ != TGSI_SWIZZLE_Z || 643 src->Register.SwizzleW != TGSI_SWIZZLE_W) { 644 CHR( '.' ); 645 ENM( src->Register.SwizzleX, swizzle_names ); 646 ENM( src->Register.SwizzleY, swizzle_names ); 647 ENM( src->Register.SwizzleZ, swizzle_names ); 648 ENM( src->Register.SwizzleW, swizzle_names ); 649 } 650 651 if (src->Register.Absolute) 652 CHR( '|' ); 653 654 first_reg = FALSE; 655 } 656 657 if (inst->Instruction.Texture) { 658 TXT( ", " ); 659 ENM( inst->Texture.Texture, texture_names ); 660 } 661 662 switch (inst->Instruction.Opcode) { 663 case TGSI_OPCODE_IF: 664 case TGSI_OPCODE_ELSE: 665 case TGSI_OPCODE_BGNLOOP: 666 case TGSI_OPCODE_ENDLOOP: 667 case TGSI_OPCODE_CAL: 668 TXT( " :" ); 669 UID( inst->Label.Label ); 670 break; 671 } 672 673 /* update indentation */ 674 if (inst->Instruction.Opcode == TGSI_OPCODE_IF || 675 inst->Instruction.Opcode == TGSI_OPCODE_ELSE || 676 inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) { 677 ctx->indentation += indent_spaces; 678 } 679 680 EOL(); 681 682 return TRUE; 683} 684 685void 686tgsi_dump_instruction( 687 const struct tgsi_full_instruction *inst, 688 uint instno ) 689{ 690 struct dump_ctx ctx; 691 692 ctx.instno = instno; 693 ctx.indent = 0; 694 ctx.printf = dump_ctx_printf; 695 ctx.indentation = 0; 696 697 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst ); 698} 699 700static boolean 701prolog( 702 struct tgsi_iterate_context *iter ) 703{ 704 struct dump_ctx *ctx = (struct dump_ctx *) iter; 705 ENM( iter->processor.Processor, processor_type_names ); 706 EOL(); 707 return TRUE; 708} 709 710void 711tgsi_dump( 712 const struct tgsi_token *tokens, 713 uint flags ) 714{ 715 struct dump_ctx ctx; 716 717 ctx.iter.prolog = prolog; 718 ctx.iter.iterate_instruction = iter_instruction; 719 ctx.iter.iterate_declaration = iter_declaration; 720 ctx.iter.iterate_immediate = iter_immediate; 721 ctx.iter.iterate_property = iter_property; 722 ctx.iter.epilog = NULL; 723 724 ctx.instno = 0; 725 ctx.indent = 0; 726 ctx.printf = dump_ctx_printf; 727 ctx.indentation = 0; 728 729 tgsi_iterate_shader( tokens, &ctx.iter ); 730} 731 732struct str_dump_ctx 733{ 734 struct dump_ctx base; 735 char *str; 736 char *ptr; 737 int left; 738}; 739 740static void 741str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...) 742{ 743 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx; 744 745 if(sctx->left > 1) { 746 int written; 747 va_list ap; 748 va_start(ap, format); 749 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap); 750 va_end(ap); 751 752 /* Some complicated logic needed to handle the return value of 753 * vsnprintf: 754 */ 755 if (written > 0) { 756 written = MIN2(sctx->left, written); 757 sctx->ptr += written; 758 sctx->left -= written; 759 } 760 } 761} 762 763void 764tgsi_dump_str( 765 const struct tgsi_token *tokens, 766 uint flags, 767 char *str, 768 size_t size) 769{ 770 struct str_dump_ctx ctx; 771 772 ctx.base.iter.prolog = prolog; 773 ctx.base.iter.iterate_instruction = iter_instruction; 774 ctx.base.iter.iterate_declaration = iter_declaration; 775 ctx.base.iter.iterate_immediate = iter_immediate; 776 ctx.base.iter.iterate_property = iter_property; 777 ctx.base.iter.epilog = NULL; 778 779 ctx.base.instno = 0; 780 ctx.base.indent = 0; 781 ctx.base.printf = &str_dump_ctx_printf; 782 ctx.base.indentation = 0; 783 784 ctx.str = str; 785 ctx.str[0] = 0; 786 ctx.ptr = str; 787 ctx.left = (int)size; 788 789 tgsi_iterate_shader( tokens, &ctx.base.iter ); 790} 791