tgsi_dump.c revision cdc920a0
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 "LOOP", 104 "PRED", 105 "SV" 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 CHR('['); 216 SID(src->Dimension.Index); 217 CHR(']'); 218 } 219 if (src->Register.Indirect) { 220 CHR( '[' ); 221 ENM( src->Indirect.File, file_names ); 222 CHR( '[' ); 223 SID( src->Indirect.Index ); 224 TXT( "]." ); 225 ENM( src->Indirect.SwizzleX, swizzle_names ); 226 if (src->Register.Index != 0) { 227 if (src->Register.Index > 0) 228 CHR( '+' ); 229 SID( src->Register.Index ); 230 } 231 CHR( ']' ); 232 } else { 233 CHR( '[' ); 234 SID( src->Register.Index ); 235 CHR( ']' ); 236 } 237} 238 239static void 240_dump_register_ind( 241 struct dump_ctx *ctx, 242 uint file, 243 int index, 244 uint ind_file, 245 int ind_index, 246 uint ind_swizzle ) 247{ 248 ENM( file, file_names ); 249 CHR( '[' ); 250 ENM( ind_file, file_names ); 251 CHR( '[' ); 252 SID( ind_index ); 253 TXT( "]." ); 254 ENM( ind_swizzle, swizzle_names ); 255 if (index != 0) { 256 if (index > 0) 257 CHR( '+' ); 258 SID( index ); 259 } 260 CHR( ']' ); 261} 262 263static void 264_dump_writemask( 265 struct dump_ctx *ctx, 266 uint writemask ) 267{ 268 if (writemask != TGSI_WRITEMASK_XYZW) { 269 CHR( '.' ); 270 if (writemask & TGSI_WRITEMASK_X) 271 CHR( 'x' ); 272 if (writemask & TGSI_WRITEMASK_Y) 273 CHR( 'y' ); 274 if (writemask & TGSI_WRITEMASK_Z) 275 CHR( 'z' ); 276 if (writemask & TGSI_WRITEMASK_W) 277 CHR( 'w' ); 278 } 279} 280 281static boolean 282iter_declaration( 283 struct tgsi_iterate_context *iter, 284 struct tgsi_full_declaration *decl ) 285{ 286 struct dump_ctx *ctx = (struct dump_ctx *)iter; 287 288 assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT); 289 assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT); 290 291 TXT( "DCL " ); 292 293 ENM(decl->Declaration.File, file_names); 294 295 /* all geometry shader inputs are two dimensional */ 296 if (decl->Declaration.File == TGSI_FILE_INPUT && 297 iter->processor.Processor == TGSI_PROCESSOR_GEOMETRY) { 298 TXT("[]"); 299 } 300 301 if (decl->Declaration.Dimension) { 302 CHR('['); 303 SID(decl->Dim.Index2D); 304 CHR(']'); 305 } 306 307 CHR('['); 308 SID(decl->Range.First); 309 if (decl->Range.First != decl->Range.Last) { 310 TXT(".."); 311 SID(decl->Range.Last); 312 } 313 CHR(']'); 314 315 _dump_writemask( 316 ctx, 317 decl->Declaration.UsageMask ); 318 319 if (decl->Declaration.Semantic) { 320 TXT( ", " ); 321 ENM( decl->Semantic.Name, semantic_names ); 322 if (decl->Semantic.Index != 0 || 323 decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) { 324 CHR( '[' ); 325 UID( decl->Semantic.Index ); 326 CHR( ']' ); 327 } 328 } 329 330 if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT && 331 decl->Declaration.File == TGSI_FILE_INPUT) 332 { 333 TXT( ", " ); 334 ENM( decl->Declaration.Interpolate, interpolate_names ); 335 } 336 337 if (decl->Declaration.Centroid) { 338 TXT( ", CENTROID" ); 339 } 340 341 if (decl->Declaration.Invariant) { 342 TXT( ", INVARIANT" ); 343 } 344 345 if (decl->Declaration.CylindricalWrap) { 346 TXT(", CYLWRAP_"); 347 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_X) { 348 CHR('X'); 349 } 350 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Y) { 351 CHR('Y'); 352 } 353 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Z) { 354 CHR('Z'); 355 } 356 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_W) { 357 CHR('W'); 358 } 359 } 360 361 EOL(); 362 363 return TRUE; 364} 365 366void 367tgsi_dump_declaration( 368 const struct tgsi_full_declaration *decl ) 369{ 370 struct dump_ctx ctx; 371 372 ctx.printf = dump_ctx_printf; 373 374 iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl ); 375} 376 377static boolean 378iter_property( 379 struct tgsi_iterate_context *iter, 380 struct tgsi_full_property *prop ) 381{ 382 int i; 383 struct dump_ctx *ctx = (struct dump_ctx *)iter; 384 385 assert(Elements(property_names) == TGSI_PROPERTY_COUNT); 386 387 TXT( "PROPERTY " ); 388 ENM(prop->Property.PropertyName, property_names); 389 390 if (prop->Property.NrTokens > 1) 391 TXT(" "); 392 393 for (i = 0; i < prop->Property.NrTokens - 1; ++i) { 394 switch (prop->Property.PropertyName) { 395 case TGSI_PROPERTY_GS_INPUT_PRIM: 396 case TGSI_PROPERTY_GS_OUTPUT_PRIM: 397 ENM(prop->u[i].Data, primitive_names); 398 break; 399 case TGSI_PROPERTY_FS_COORD_ORIGIN: 400 ENM(prop->u[i].Data, fs_coord_origin_names); 401 break; 402 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER: 403 ENM(prop->u[i].Data, fs_coord_pixel_center_names); 404 break; 405 default: 406 SID( prop->u[i].Data ); 407 break; 408 } 409 if (i < prop->Property.NrTokens - 2) 410 TXT( ", " ); 411 } 412 EOL(); 413 414 return TRUE; 415} 416 417void tgsi_dump_property( 418 const struct tgsi_full_property *prop ) 419{ 420 struct dump_ctx ctx; 421 422 ctx.printf = dump_ctx_printf; 423 424 iter_property( &ctx.iter, (struct tgsi_full_property *)prop ); 425} 426 427static boolean 428iter_immediate( 429 struct tgsi_iterate_context *iter, 430 struct tgsi_full_immediate *imm ) 431{ 432 struct dump_ctx *ctx = (struct dump_ctx *) iter; 433 434 uint i; 435 436 TXT( "IMM " ); 437 ENM( imm->Immediate.DataType, immediate_type_names ); 438 439 TXT( " { " ); 440 441 assert( imm->Immediate.NrTokens <= 4 + 1 ); 442 for (i = 0; i < imm->Immediate.NrTokens - 1; i++) { 443 switch (imm->Immediate.DataType) { 444 case TGSI_IMM_FLOAT32: 445 FLT( imm->u[i].Float ); 446 break; 447 case TGSI_IMM_UINT32: 448 UID(imm->u[i].Uint); 449 break; 450 case TGSI_IMM_INT32: 451 SID(imm->u[i].Int); 452 break; 453 default: 454 assert( 0 ); 455 } 456 457 if (i < imm->Immediate.NrTokens - 2) 458 TXT( ", " ); 459 } 460 TXT( " }" ); 461 462 EOL(); 463 464 return TRUE; 465} 466 467void 468tgsi_dump_immediate( 469 const struct tgsi_full_immediate *imm ) 470{ 471 struct dump_ctx ctx; 472 473 ctx.printf = dump_ctx_printf; 474 475 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm ); 476} 477 478static boolean 479iter_instruction( 480 struct tgsi_iterate_context *iter, 481 struct tgsi_full_instruction *inst ) 482{ 483 struct dump_ctx *ctx = (struct dump_ctx *) iter; 484 uint instno = ctx->instno++; 485 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode ); 486 uint i; 487 boolean first_reg = TRUE; 488 489 INSTID( instno ); 490 TXT( ": " ); 491 492 ctx->indent -= info->pre_dedent; 493 for(i = 0; (int)i < ctx->indent; ++i) 494 TXT( " " ); 495 ctx->indent += info->post_indent; 496 497 TXT( info->mnemonic ); 498 499 switch (inst->Instruction.Saturate) { 500 case TGSI_SAT_NONE: 501 break; 502 case TGSI_SAT_ZERO_ONE: 503 TXT( "_SAT" ); 504 break; 505 case TGSI_SAT_MINUS_PLUS_ONE: 506 TXT( "_SATNV" ); 507 break; 508 default: 509 assert( 0 ); 510 } 511 512 for (i = 0; i < inst->Instruction.NumDstRegs; i++) { 513 const struct tgsi_full_dst_register *dst = &inst->Dst[i]; 514 515 if (!first_reg) 516 CHR( ',' ); 517 CHR( ' ' ); 518 519 if (dst->Register.Indirect) { 520 _dump_register_ind( 521 ctx, 522 dst->Register.File, 523 dst->Register.Index, 524 dst->Indirect.File, 525 dst->Indirect.Index, 526 dst->Indirect.SwizzleX ); 527 } 528 else { 529 _dump_register_dst( 530 ctx, 531 dst->Register.File, 532 dst->Register.Index ); 533 } 534 _dump_writemask( ctx, dst->Register.WriteMask ); 535 536 first_reg = FALSE; 537 } 538 539 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) { 540 const struct tgsi_full_src_register *src = &inst->Src[i]; 541 542 if (!first_reg) 543 CHR( ',' ); 544 CHR( ' ' ); 545 546 if (src->Register.Negate) 547 CHR( '-' ); 548 if (src->Register.Absolute) 549 CHR( '|' ); 550 551 _dump_register_src(ctx, src); 552 553 if (src->Register.SwizzleX != TGSI_SWIZZLE_X || 554 src->Register.SwizzleY != TGSI_SWIZZLE_Y || 555 src->Register.SwizzleZ != TGSI_SWIZZLE_Z || 556 src->Register.SwizzleW != TGSI_SWIZZLE_W) { 557 CHR( '.' ); 558 ENM( src->Register.SwizzleX, swizzle_names ); 559 ENM( src->Register.SwizzleY, swizzle_names ); 560 ENM( src->Register.SwizzleZ, swizzle_names ); 561 ENM( src->Register.SwizzleW, swizzle_names ); 562 } 563 564 if (src->Register.Absolute) 565 CHR( '|' ); 566 567 first_reg = FALSE; 568 } 569 570 if (inst->Instruction.Texture) { 571 TXT( ", " ); 572 ENM( inst->Texture.Texture, texture_names ); 573 } 574 575 switch (inst->Instruction.Opcode) { 576 case TGSI_OPCODE_IF: 577 case TGSI_OPCODE_ELSE: 578 case TGSI_OPCODE_BGNLOOP: 579 case TGSI_OPCODE_ENDLOOP: 580 case TGSI_OPCODE_CAL: 581 TXT( " :" ); 582 UID( inst->Label.Label ); 583 break; 584 } 585 586 /* update indentation */ 587 if (inst->Instruction.Opcode == TGSI_OPCODE_IF || 588 inst->Instruction.Opcode == TGSI_OPCODE_ELSE || 589 inst->Instruction.Opcode == TGSI_OPCODE_BGNFOR || 590 inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) { 591 ctx->indentation += indent_spaces; 592 } 593 594 EOL(); 595 596 return TRUE; 597} 598 599void 600tgsi_dump_instruction( 601 const struct tgsi_full_instruction *inst, 602 uint instno ) 603{ 604 struct dump_ctx ctx; 605 606 ctx.instno = instno; 607 ctx.indent = 0; 608 ctx.printf = dump_ctx_printf; 609 ctx.indentation = 0; 610 611 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst ); 612} 613 614static boolean 615prolog( 616 struct tgsi_iterate_context *iter ) 617{ 618 struct dump_ctx *ctx = (struct dump_ctx *) iter; 619 ENM( iter->processor.Processor, processor_type_names ); 620 EOL(); 621 return TRUE; 622} 623 624void 625tgsi_dump( 626 const struct tgsi_token *tokens, 627 uint flags ) 628{ 629 struct dump_ctx ctx; 630 631 ctx.iter.prolog = prolog; 632 ctx.iter.iterate_instruction = iter_instruction; 633 ctx.iter.iterate_declaration = iter_declaration; 634 ctx.iter.iterate_immediate = iter_immediate; 635 ctx.iter.iterate_property = iter_property; 636 ctx.iter.epilog = NULL; 637 638 ctx.instno = 0; 639 ctx.indent = 0; 640 ctx.printf = dump_ctx_printf; 641 ctx.indentation = 0; 642 643 tgsi_iterate_shader( tokens, &ctx.iter ); 644} 645 646struct str_dump_ctx 647{ 648 struct dump_ctx base; 649 char *str; 650 char *ptr; 651 int left; 652}; 653 654static void 655str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...) 656{ 657 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx; 658 659 if(sctx->left > 1) { 660 int written; 661 va_list ap; 662 va_start(ap, format); 663 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap); 664 va_end(ap); 665 666 /* Some complicated logic needed to handle the return value of 667 * vsnprintf: 668 */ 669 if (written > 0) { 670 written = MIN2(sctx->left, written); 671 sctx->ptr += written; 672 sctx->left -= written; 673 } 674 } 675} 676 677void 678tgsi_dump_str( 679 const struct tgsi_token *tokens, 680 uint flags, 681 char *str, 682 size_t size) 683{ 684 struct str_dump_ctx ctx; 685 686 ctx.base.iter.prolog = prolog; 687 ctx.base.iter.iterate_instruction = iter_instruction; 688 ctx.base.iter.iterate_declaration = iter_declaration; 689 ctx.base.iter.iterate_immediate = iter_immediate; 690 ctx.base.iter.iterate_property = iter_property; 691 ctx.base.iter.epilog = NULL; 692 693 ctx.base.instno = 0; 694 ctx.base.indent = 0; 695 ctx.base.printf = &str_dump_ctx_printf; 696 ctx.base.indentation = 0; 697 698 ctx.str = str; 699 ctx.str[0] = 0; 700 ctx.ptr = str; 701 ctx.left = (int)size; 702 703 tgsi_iterate_shader( tokens, &ctx.base.iter ); 704} 705