tgsi_dump.c revision 73317139a4f78126af0dc4ddaef8206352740727
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 EOL(); 346 347 return TRUE; 348} 349 350void 351tgsi_dump_declaration( 352 const struct tgsi_full_declaration *decl ) 353{ 354 struct dump_ctx ctx; 355 356 ctx.printf = dump_ctx_printf; 357 358 iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl ); 359} 360 361static boolean 362iter_property( 363 struct tgsi_iterate_context *iter, 364 struct tgsi_full_property *prop ) 365{ 366 int i; 367 struct dump_ctx *ctx = (struct dump_ctx *)iter; 368 369 assert(Elements(property_names) == TGSI_PROPERTY_COUNT); 370 371 TXT( "PROPERTY " ); 372 ENM(prop->Property.PropertyName, property_names); 373 374 if (prop->Property.NrTokens > 1) 375 TXT(" "); 376 377 for (i = 0; i < prop->Property.NrTokens - 1; ++i) { 378 switch (prop->Property.PropertyName) { 379 case TGSI_PROPERTY_GS_INPUT_PRIM: 380 case TGSI_PROPERTY_GS_OUTPUT_PRIM: 381 ENM(prop->u[i].Data, primitive_names); 382 break; 383 case TGSI_PROPERTY_FS_COORD_ORIGIN: 384 ENM(prop->u[i].Data, fs_coord_origin_names); 385 break; 386 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER: 387 ENM(prop->u[i].Data, fs_coord_pixel_center_names); 388 break; 389 default: 390 SID( prop->u[i].Data ); 391 break; 392 } 393 if (i < prop->Property.NrTokens - 2) 394 TXT( ", " ); 395 } 396 EOL(); 397 398 return TRUE; 399} 400 401void tgsi_dump_property( 402 const struct tgsi_full_property *prop ) 403{ 404 struct dump_ctx ctx; 405 406 ctx.printf = dump_ctx_printf; 407 408 iter_property( &ctx.iter, (struct tgsi_full_property *)prop ); 409} 410 411static boolean 412iter_immediate( 413 struct tgsi_iterate_context *iter, 414 struct tgsi_full_immediate *imm ) 415{ 416 struct dump_ctx *ctx = (struct dump_ctx *) iter; 417 418 uint i; 419 420 TXT( "IMM " ); 421 ENM( imm->Immediate.DataType, immediate_type_names ); 422 423 TXT( " { " ); 424 425 assert( imm->Immediate.NrTokens <= 4 + 1 ); 426 for (i = 0; i < imm->Immediate.NrTokens - 1; i++) { 427 switch (imm->Immediate.DataType) { 428 case TGSI_IMM_FLOAT32: 429 FLT( imm->u[i].Float ); 430 break; 431 case TGSI_IMM_UINT32: 432 UID(imm->u[i].Uint); 433 break; 434 case TGSI_IMM_INT32: 435 SID(imm->u[i].Int); 436 break; 437 default: 438 assert( 0 ); 439 } 440 441 if (i < imm->Immediate.NrTokens - 2) 442 TXT( ", " ); 443 } 444 TXT( " }" ); 445 446 EOL(); 447 448 return TRUE; 449} 450 451void 452tgsi_dump_immediate( 453 const struct tgsi_full_immediate *imm ) 454{ 455 struct dump_ctx ctx; 456 457 ctx.printf = dump_ctx_printf; 458 459 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm ); 460} 461 462static boolean 463iter_instruction( 464 struct tgsi_iterate_context *iter, 465 struct tgsi_full_instruction *inst ) 466{ 467 struct dump_ctx *ctx = (struct dump_ctx *) iter; 468 uint instno = ctx->instno++; 469 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode ); 470 uint i; 471 boolean first_reg = TRUE; 472 473 INSTID( instno ); 474 TXT( ": " ); 475 476 ctx->indent -= info->pre_dedent; 477 for(i = 0; (int)i < ctx->indent; ++i) 478 TXT( " " ); 479 ctx->indent += info->post_indent; 480 481 TXT( info->mnemonic ); 482 483 switch (inst->Instruction.Saturate) { 484 case TGSI_SAT_NONE: 485 break; 486 case TGSI_SAT_ZERO_ONE: 487 TXT( "_SAT" ); 488 break; 489 case TGSI_SAT_MINUS_PLUS_ONE: 490 TXT( "_SATNV" ); 491 break; 492 default: 493 assert( 0 ); 494 } 495 496 for (i = 0; i < inst->Instruction.NumDstRegs; i++) { 497 const struct tgsi_full_dst_register *dst = &inst->Dst[i]; 498 499 if (!first_reg) 500 CHR( ',' ); 501 CHR( ' ' ); 502 503 if (dst->Register.Indirect) { 504 _dump_register_ind( 505 ctx, 506 dst->Register.File, 507 dst->Register.Index, 508 dst->Indirect.File, 509 dst->Indirect.Index, 510 dst->Indirect.SwizzleX ); 511 } 512 else { 513 _dump_register_dst( 514 ctx, 515 dst->Register.File, 516 dst->Register.Index ); 517 } 518 _dump_writemask( ctx, dst->Register.WriteMask ); 519 520 first_reg = FALSE; 521 } 522 523 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) { 524 const struct tgsi_full_src_register *src = &inst->Src[i]; 525 526 if (!first_reg) 527 CHR( ',' ); 528 CHR( ' ' ); 529 530 if (src->Register.Negate) 531 CHR( '-' ); 532 if (src->Register.Absolute) 533 CHR( '|' ); 534 535 _dump_register_src(ctx, src); 536 537 if (src->Register.SwizzleX != TGSI_SWIZZLE_X || 538 src->Register.SwizzleY != TGSI_SWIZZLE_Y || 539 src->Register.SwizzleZ != TGSI_SWIZZLE_Z || 540 src->Register.SwizzleW != TGSI_SWIZZLE_W) { 541 CHR( '.' ); 542 ENM( src->Register.SwizzleX, swizzle_names ); 543 ENM( src->Register.SwizzleY, swizzle_names ); 544 ENM( src->Register.SwizzleZ, swizzle_names ); 545 ENM( src->Register.SwizzleW, swizzle_names ); 546 } 547 548 if (src->Register.Absolute) 549 CHR( '|' ); 550 551 first_reg = FALSE; 552 } 553 554 if (inst->Instruction.Texture) { 555 TXT( ", " ); 556 ENM( inst->Texture.Texture, texture_names ); 557 } 558 559 switch (inst->Instruction.Opcode) { 560 case TGSI_OPCODE_IF: 561 case TGSI_OPCODE_ELSE: 562 case TGSI_OPCODE_BGNLOOP: 563 case TGSI_OPCODE_ENDLOOP: 564 case TGSI_OPCODE_CAL: 565 TXT( " :" ); 566 UID( inst->Label.Label ); 567 break; 568 } 569 570 /* update indentation */ 571 if (inst->Instruction.Opcode == TGSI_OPCODE_IF || 572 inst->Instruction.Opcode == TGSI_OPCODE_ELSE || 573 inst->Instruction.Opcode == TGSI_OPCODE_BGNFOR || 574 inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) { 575 ctx->indentation += indent_spaces; 576 } 577 578 EOL(); 579 580 return TRUE; 581} 582 583void 584tgsi_dump_instruction( 585 const struct tgsi_full_instruction *inst, 586 uint instno ) 587{ 588 struct dump_ctx ctx; 589 590 ctx.instno = instno; 591 ctx.indent = 0; 592 ctx.printf = dump_ctx_printf; 593 ctx.indentation = 0; 594 595 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst ); 596} 597 598static boolean 599prolog( 600 struct tgsi_iterate_context *iter ) 601{ 602 struct dump_ctx *ctx = (struct dump_ctx *) iter; 603 ENM( iter->processor.Processor, processor_type_names ); 604 EOL(); 605 return TRUE; 606} 607 608void 609tgsi_dump( 610 const struct tgsi_token *tokens, 611 uint flags ) 612{ 613 struct dump_ctx ctx; 614 615 ctx.iter.prolog = prolog; 616 ctx.iter.iterate_instruction = iter_instruction; 617 ctx.iter.iterate_declaration = iter_declaration; 618 ctx.iter.iterate_immediate = iter_immediate; 619 ctx.iter.iterate_property = iter_property; 620 ctx.iter.epilog = NULL; 621 622 ctx.instno = 0; 623 ctx.indent = 0; 624 ctx.printf = dump_ctx_printf; 625 ctx.indentation = 0; 626 627 tgsi_iterate_shader( tokens, &ctx.iter ); 628} 629 630struct str_dump_ctx 631{ 632 struct dump_ctx base; 633 char *str; 634 char *ptr; 635 int left; 636}; 637 638static void 639str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...) 640{ 641 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx; 642 643 if(sctx->left > 1) { 644 int written; 645 va_list ap; 646 va_start(ap, format); 647 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap); 648 va_end(ap); 649 650 /* Some complicated logic needed to handle the return value of 651 * vsnprintf: 652 */ 653 if (written > 0) { 654 written = MIN2(sctx->left, written); 655 sctx->ptr += written; 656 sctx->left -= written; 657 } 658 } 659} 660 661void 662tgsi_dump_str( 663 const struct tgsi_token *tokens, 664 uint flags, 665 char *str, 666 size_t size) 667{ 668 struct str_dump_ctx ctx; 669 670 ctx.base.iter.prolog = prolog; 671 ctx.base.iter.iterate_instruction = iter_instruction; 672 ctx.base.iter.iterate_declaration = iter_declaration; 673 ctx.base.iter.iterate_immediate = iter_immediate; 674 ctx.base.iter.iterate_property = iter_property; 675 ctx.base.iter.epilog = NULL; 676 677 ctx.base.instno = 0; 678 ctx.base.indent = 0; 679 ctx.base.printf = &str_dump_ctx_printf; 680 ctx.base.indentation = 0; 681 682 ctx.str = str; 683 ctx.str[0] = 0; 684 ctx.ptr = str; 685 ctx.left = (int)size; 686 687 tgsi_iterate_shader( tokens, &ctx.base.iter ); 688} 689