tgsi_dump.c revision 82c90b2da8e236bc134cb2bd0ec4e09551b12164
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#include "tgsi_strings.h" 36 37 38/** Number of spaces to indent for IF/LOOP/etc */ 39static const int indent_spaces = 3; 40 41 42struct dump_ctx 43{ 44 struct tgsi_iterate_context iter; 45 46 uint instno; 47 int indent; 48 49 uint indentation; 50 51 void (*dump_printf)(struct dump_ctx *ctx, const char *format, ...); 52}; 53 54static void 55dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...) 56{ 57 va_list ap; 58 (void)ctx; 59 va_start(ap, format); 60 _debug_vprintf(format, ap); 61 va_end(ap); 62} 63 64static void 65dump_enum( 66 struct dump_ctx *ctx, 67 uint e, 68 const char **enums, 69 uint enum_count ) 70{ 71 if (e >= enum_count) 72 ctx->dump_printf( ctx, "%u", e ); 73 else 74 ctx->dump_printf( ctx, "%s", enums[e] ); 75} 76 77#define EOL() ctx->dump_printf( ctx, "\n" ) 78#define TXT(S) ctx->dump_printf( ctx, "%s", S ) 79#define CHR(C) ctx->dump_printf( ctx, "%c", C ) 80#define UIX(I) ctx->dump_printf( ctx, "0x%x", I ) 81#define UID(I) ctx->dump_printf( ctx, "%u", I ) 82#define INSTID(I) ctx->dump_printf( ctx, "% 3u", I ) 83#define SID(I) ctx->dump_printf( ctx, "%d", I ) 84#define FLT(F) ctx->dump_printf( ctx, "%10.4f", F ) 85#define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) ) 86 87const char * 88tgsi_swizzle_names[4] = 89{ 90 "x", 91 "y", 92 "z", 93 "w" 94}; 95 96static void 97_dump_register_src( 98 struct dump_ctx *ctx, 99 const struct tgsi_full_src_register *src ) 100{ 101 ENM(src->Register.File, tgsi_file_names); 102 if (src->Register.Dimension) { 103 if (src->Dimension.Indirect) { 104 CHR( '[' ); 105 ENM( src->DimIndirect.File, tgsi_file_names ); 106 CHR( '[' ); 107 SID( src->DimIndirect.Index ); 108 TXT( "]." ); 109 ENM( src->DimIndirect.SwizzleX, tgsi_swizzle_names ); 110 if (src->Dimension.Index != 0) { 111 if (src->Dimension.Index > 0) 112 CHR( '+' ); 113 SID( src->Dimension.Index ); 114 } 115 CHR( ']' ); 116 } else { 117 CHR('['); 118 SID(src->Dimension.Index); 119 CHR(']'); 120 } 121 } 122 if (src->Register.Indirect) { 123 CHR( '[' ); 124 ENM( src->Indirect.File, tgsi_file_names ); 125 CHR( '[' ); 126 SID( src->Indirect.Index ); 127 TXT( "]." ); 128 ENM( src->Indirect.SwizzleX, tgsi_swizzle_names ); 129 if (src->Register.Index != 0) { 130 if (src->Register.Index > 0) 131 CHR( '+' ); 132 SID( src->Register.Index ); 133 } 134 CHR( ']' ); 135 } else { 136 CHR( '[' ); 137 SID( src->Register.Index ); 138 CHR( ']' ); 139 } 140} 141 142 143static void 144_dump_register_dst( 145 struct dump_ctx *ctx, 146 const struct tgsi_full_dst_register *dst ) 147{ 148 ENM(dst->Register.File, tgsi_file_names); 149 if (dst->Register.Dimension) { 150 if (dst->Dimension.Indirect) { 151 CHR( '[' ); 152 ENM( dst->DimIndirect.File, tgsi_file_names ); 153 CHR( '[' ); 154 SID( dst->DimIndirect.Index ); 155 TXT( "]." ); 156 ENM( dst->DimIndirect.SwizzleX, tgsi_swizzle_names ); 157 if (dst->Dimension.Index != 0) { 158 if (dst->Dimension.Index > 0) 159 CHR( '+' ); 160 SID( dst->Dimension.Index ); 161 } 162 CHR( ']' ); 163 } else { 164 CHR('['); 165 SID(dst->Dimension.Index); 166 CHR(']'); 167 } 168 } 169 if (dst->Register.Indirect) { 170 CHR( '[' ); 171 ENM( dst->Indirect.File, tgsi_file_names ); 172 CHR( '[' ); 173 SID( dst->Indirect.Index ); 174 TXT( "]." ); 175 ENM( dst->Indirect.SwizzleX, tgsi_swizzle_names ); 176 if (dst->Register.Index != 0) { 177 if (dst->Register.Index > 0) 178 CHR( '+' ); 179 SID( dst->Register.Index ); 180 } 181 CHR( ']' ); 182 } else { 183 CHR( '[' ); 184 SID( dst->Register.Index ); 185 CHR( ']' ); 186 } 187} 188static void 189_dump_writemask( 190 struct dump_ctx *ctx, 191 uint writemask ) 192{ 193 if (writemask != TGSI_WRITEMASK_XYZW) { 194 CHR( '.' ); 195 if (writemask & TGSI_WRITEMASK_X) 196 CHR( 'x' ); 197 if (writemask & TGSI_WRITEMASK_Y) 198 CHR( 'y' ); 199 if (writemask & TGSI_WRITEMASK_Z) 200 CHR( 'z' ); 201 if (writemask & TGSI_WRITEMASK_W) 202 CHR( 'w' ); 203 } 204} 205 206static void 207dump_imm_data(struct tgsi_iterate_context *iter, 208 union tgsi_immediate_data *data, 209 unsigned num_tokens, 210 unsigned data_type) 211{ 212 struct dump_ctx *ctx = (struct dump_ctx *)iter; 213 unsigned i ; 214 215 TXT( " {" ); 216 217 assert( num_tokens <= 4 ); 218 for (i = 0; i < num_tokens; i++) { 219 switch (data_type) { 220 case TGSI_IMM_FLOAT32: 221 FLT( data[i].Float ); 222 break; 223 case TGSI_IMM_UINT32: 224 UID(data[i].Uint); 225 break; 226 case TGSI_IMM_INT32: 227 SID(data[i].Int); 228 break; 229 default: 230 assert( 0 ); 231 } 232 233 if (i < num_tokens - 1) 234 TXT( ", " ); 235 } 236 TXT( "}" ); 237} 238 239static boolean 240iter_declaration( 241 struct tgsi_iterate_context *iter, 242 struct tgsi_full_declaration *decl ) 243{ 244 struct dump_ctx *ctx = (struct dump_ctx *)iter; 245 246 TXT( "DCL " ); 247 248 ENM(decl->Declaration.File, tgsi_file_names); 249 250 /* all geometry shader inputs are two dimensional */ 251 if (decl->Declaration.File == TGSI_FILE_INPUT && 252 iter->processor.Processor == TGSI_PROCESSOR_GEOMETRY) { 253 TXT("[]"); 254 } 255 256 if (decl->Declaration.Dimension) { 257 CHR('['); 258 SID(decl->Dim.Index2D); 259 CHR(']'); 260 } 261 262 CHR('['); 263 SID(decl->Range.First); 264 if (decl->Range.First != decl->Range.Last) { 265 TXT(".."); 266 SID(decl->Range.Last); 267 } 268 CHR(']'); 269 270 _dump_writemask( 271 ctx, 272 decl->Declaration.UsageMask ); 273 274 if (decl->Declaration.Semantic) { 275 TXT( ", " ); 276 ENM( decl->Semantic.Name, tgsi_semantic_names ); 277 if (decl->Semantic.Index != 0 || 278 decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) { 279 CHR( '[' ); 280 UID( decl->Semantic.Index ); 281 CHR( ']' ); 282 } 283 } 284 285 if (decl->Declaration.File == TGSI_FILE_RESOURCE) { 286 TXT(", "); 287 ENM(decl->Resource.Resource, tgsi_texture_names); 288 if (decl->Resource.Raw) 289 TXT(", RAW"); 290 } 291 292 if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) { 293 TXT(", "); 294 ENM(decl->SamplerView.Resource, tgsi_texture_names); 295 TXT(", "); 296 if ((decl->SamplerView.ReturnTypeX == decl->SamplerView.ReturnTypeY) && 297 (decl->SamplerView.ReturnTypeX == decl->SamplerView.ReturnTypeZ) && 298 (decl->SamplerView.ReturnTypeX == decl->SamplerView.ReturnTypeW)) { 299 ENM(decl->SamplerView.ReturnTypeX, tgsi_type_names); 300 } else { 301 ENM(decl->SamplerView.ReturnTypeX, tgsi_type_names); 302 TXT(", "); 303 ENM(decl->SamplerView.ReturnTypeY, tgsi_type_names); 304 TXT(", "); 305 ENM(decl->SamplerView.ReturnTypeZ, tgsi_type_names); 306 TXT(", "); 307 ENM(decl->SamplerView.ReturnTypeW, tgsi_type_names); 308 } 309 } 310 311 if (decl->Declaration.Interpolate) { 312 if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT && 313 decl->Declaration.File == TGSI_FILE_INPUT) 314 { 315 TXT( ", " ); 316 ENM( decl->Interp.Interpolate, tgsi_interpolate_names ); 317 } 318 319 if (decl->Interp.Centroid) { 320 TXT( ", CENTROID" ); 321 } 322 323 if (decl->Interp.CylindricalWrap) { 324 TXT(", CYLWRAP_"); 325 if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_X) { 326 CHR('X'); 327 } 328 if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Y) { 329 CHR('Y'); 330 } 331 if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Z) { 332 CHR('Z'); 333 } 334 if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_W) { 335 CHR('W'); 336 } 337 } 338 } 339 340 if (decl->Declaration.Invariant) { 341 TXT( ", INVARIANT" ); 342 } 343 344 345 if (decl->Declaration.File == TGSI_FILE_IMMEDIATE_ARRAY) { 346 unsigned i; 347 char range_indent[4]; 348 349 TXT(" {"); 350 351 if (decl->Range.Last < 10) 352 range_indent[0] = '\0'; 353 else if (decl->Range.Last < 100) { 354 range_indent[0] = ' '; 355 range_indent[1] = '\0'; 356 } else if (decl->Range.Last < 1000) { 357 range_indent[0] = ' '; 358 range_indent[1] = ' '; 359 range_indent[2] = '\0'; 360 } else { 361 range_indent[0] = ' '; 362 range_indent[1] = ' '; 363 range_indent[2] = ' '; 364 range_indent[3] = '\0'; 365 } 366 367 dump_imm_data(iter, decl->ImmediateData.u, 368 4, TGSI_IMM_FLOAT32); 369 for(i = 1; i <= decl->Range.Last; ++i) { 370 /* indent by strlen of: 371 * "DCL IMMX[0..1] {" */ 372 CHR('\n'); 373 TXT( " " ); 374 TXT( range_indent ); 375 dump_imm_data(iter, decl->ImmediateData.u + i, 376 4, TGSI_IMM_FLOAT32); 377 } 378 379 TXT(" }"); 380 } 381 382 EOL(); 383 384 return TRUE; 385} 386 387void 388tgsi_dump_declaration( 389 const struct tgsi_full_declaration *decl ) 390{ 391 struct dump_ctx ctx; 392 393 ctx.dump_printf = dump_ctx_printf; 394 395 iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl ); 396} 397 398static boolean 399iter_property( 400 struct tgsi_iterate_context *iter, 401 struct tgsi_full_property *prop ) 402{ 403 int i; 404 struct dump_ctx *ctx = (struct dump_ctx *)iter; 405 406 TXT( "PROPERTY " ); 407 ENM(prop->Property.PropertyName, tgsi_property_names); 408 409 if (prop->Property.NrTokens > 1) 410 TXT(" "); 411 412 for (i = 0; i < prop->Property.NrTokens - 1; ++i) { 413 switch (prop->Property.PropertyName) { 414 case TGSI_PROPERTY_GS_INPUT_PRIM: 415 case TGSI_PROPERTY_GS_OUTPUT_PRIM: 416 ENM(prop->u[i].Data, tgsi_primitive_names); 417 break; 418 case TGSI_PROPERTY_FS_COORD_ORIGIN: 419 ENM(prop->u[i].Data, tgsi_fs_coord_origin_names); 420 break; 421 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER: 422 ENM(prop->u[i].Data, tgsi_fs_coord_pixel_center_names); 423 break; 424 default: 425 SID( prop->u[i].Data ); 426 break; 427 } 428 if (i < prop->Property.NrTokens - 2) 429 TXT( ", " ); 430 } 431 EOL(); 432 433 return TRUE; 434} 435 436void tgsi_dump_property( 437 const struct tgsi_full_property *prop ) 438{ 439 struct dump_ctx ctx; 440 441 ctx.dump_printf = dump_ctx_printf; 442 443 iter_property( &ctx.iter, (struct tgsi_full_property *)prop ); 444} 445 446static boolean 447iter_immediate( 448 struct tgsi_iterate_context *iter, 449 struct tgsi_full_immediate *imm ) 450{ 451 struct dump_ctx *ctx = (struct dump_ctx *) iter; 452 453 TXT( "IMM " ); 454 ENM( imm->Immediate.DataType, tgsi_immediate_type_names ); 455 456 dump_imm_data(iter, imm->u, imm->Immediate.NrTokens - 1, 457 imm->Immediate.DataType); 458 459 EOL(); 460 461 return TRUE; 462} 463 464void 465tgsi_dump_immediate( 466 const struct tgsi_full_immediate *imm ) 467{ 468 struct dump_ctx ctx; 469 470 ctx.dump_printf = dump_ctx_printf; 471 472 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm ); 473} 474 475static boolean 476iter_instruction( 477 struct tgsi_iterate_context *iter, 478 struct tgsi_full_instruction *inst ) 479{ 480 struct dump_ctx *ctx = (struct dump_ctx *) iter; 481 uint instno = ctx->instno++; 482 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode ); 483 uint i; 484 boolean first_reg = TRUE; 485 486 INSTID( instno ); 487 TXT( ": " ); 488 489 ctx->indent -= info->pre_dedent; 490 for(i = 0; (int)i < ctx->indent; ++i) 491 TXT( " " ); 492 ctx->indent += info->post_indent; 493 494 if (inst->Instruction.Predicate) { 495 CHR( '(' ); 496 497 if (inst->Predicate.Negate) 498 CHR( '!' ); 499 500 TXT( "PRED[" ); 501 SID( inst->Predicate.Index ); 502 CHR( ']' ); 503 504 if (inst->Predicate.SwizzleX != TGSI_SWIZZLE_X || 505 inst->Predicate.SwizzleY != TGSI_SWIZZLE_Y || 506 inst->Predicate.SwizzleZ != TGSI_SWIZZLE_Z || 507 inst->Predicate.SwizzleW != TGSI_SWIZZLE_W) { 508 CHR( '.' ); 509 ENM( inst->Predicate.SwizzleX, tgsi_swizzle_names ); 510 ENM( inst->Predicate.SwizzleY, tgsi_swizzle_names ); 511 ENM( inst->Predicate.SwizzleZ, tgsi_swizzle_names ); 512 ENM( inst->Predicate.SwizzleW, tgsi_swizzle_names ); 513 } 514 515 TXT( ") " ); 516 } 517 518 TXT( info->mnemonic ); 519 520 switch (inst->Instruction.Saturate) { 521 case TGSI_SAT_NONE: 522 break; 523 case TGSI_SAT_ZERO_ONE: 524 TXT( "_SAT" ); 525 break; 526 case TGSI_SAT_MINUS_PLUS_ONE: 527 TXT( "_SATNV" ); 528 break; 529 default: 530 assert( 0 ); 531 } 532 533 for (i = 0; i < inst->Instruction.NumDstRegs; i++) { 534 const struct tgsi_full_dst_register *dst = &inst->Dst[i]; 535 536 if (!first_reg) 537 CHR( ',' ); 538 CHR( ' ' ); 539 540 _dump_register_dst( ctx, dst ); 541 _dump_writemask( ctx, dst->Register.WriteMask ); 542 543 first_reg = FALSE; 544 } 545 546 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) { 547 const struct tgsi_full_src_register *src = &inst->Src[i]; 548 549 if (!first_reg) 550 CHR( ',' ); 551 CHR( ' ' ); 552 553 if (src->Register.Negate) 554 CHR( '-' ); 555 if (src->Register.Absolute) 556 CHR( '|' ); 557 558 _dump_register_src(ctx, src); 559 560 if (src->Register.SwizzleX != TGSI_SWIZZLE_X || 561 src->Register.SwizzleY != TGSI_SWIZZLE_Y || 562 src->Register.SwizzleZ != TGSI_SWIZZLE_Z || 563 src->Register.SwizzleW != TGSI_SWIZZLE_W) { 564 CHR( '.' ); 565 ENM( src->Register.SwizzleX, tgsi_swizzle_names ); 566 ENM( src->Register.SwizzleY, tgsi_swizzle_names ); 567 ENM( src->Register.SwizzleZ, tgsi_swizzle_names ); 568 ENM( src->Register.SwizzleW, tgsi_swizzle_names ); 569 } 570 571 if (src->Register.Absolute) 572 CHR( '|' ); 573 574 first_reg = FALSE; 575 } 576 577 if (inst->Instruction.Texture) { 578 TXT( ", " ); 579 ENM( inst->Texture.Texture, tgsi_texture_names ); 580 for (i = 0; i < inst->Texture.NumOffsets; i++) { 581 TXT( ", " ); 582 ENM( inst->TexOffsets[i].File, tgsi_file_names); 583 CHR( '[' ); 584 SID( inst->TexOffsets[i].Index ); 585 CHR( ']' ); 586 CHR( '.' ); 587 ENM( inst->TexOffsets[i].SwizzleX, tgsi_swizzle_names); 588 ENM( inst->TexOffsets[i].SwizzleY, tgsi_swizzle_names); 589 ENM( inst->TexOffsets[i].SwizzleZ, tgsi_swizzle_names); 590 } 591 } 592 593 switch (inst->Instruction.Opcode) { 594 case TGSI_OPCODE_IF: 595 case TGSI_OPCODE_ELSE: 596 case TGSI_OPCODE_BGNLOOP: 597 case TGSI_OPCODE_ENDLOOP: 598 case TGSI_OPCODE_CAL: 599 TXT( " :" ); 600 UID( inst->Label.Label ); 601 break; 602 } 603 604 /* update indentation */ 605 if (inst->Instruction.Opcode == TGSI_OPCODE_IF || 606 inst->Instruction.Opcode == TGSI_OPCODE_ELSE || 607 inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) { 608 ctx->indentation += indent_spaces; 609 } 610 611 EOL(); 612 613 return TRUE; 614} 615 616void 617tgsi_dump_instruction( 618 const struct tgsi_full_instruction *inst, 619 uint instno ) 620{ 621 struct dump_ctx ctx; 622 623 ctx.instno = instno; 624 ctx.indent = 0; 625 ctx.dump_printf = dump_ctx_printf; 626 ctx.indentation = 0; 627 628 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst ); 629} 630 631static boolean 632prolog( 633 struct tgsi_iterate_context *iter ) 634{ 635 struct dump_ctx *ctx = (struct dump_ctx *) iter; 636 ENM( iter->processor.Processor, tgsi_processor_type_names ); 637 EOL(); 638 return TRUE; 639} 640 641void 642tgsi_dump( 643 const struct tgsi_token *tokens, 644 uint flags ) 645{ 646 struct dump_ctx ctx; 647 648 ctx.iter.prolog = prolog; 649 ctx.iter.iterate_instruction = iter_instruction; 650 ctx.iter.iterate_declaration = iter_declaration; 651 ctx.iter.iterate_immediate = iter_immediate; 652 ctx.iter.iterate_property = iter_property; 653 ctx.iter.epilog = NULL; 654 655 ctx.instno = 0; 656 ctx.indent = 0; 657 ctx.dump_printf = dump_ctx_printf; 658 ctx.indentation = 0; 659 660 tgsi_iterate_shader( tokens, &ctx.iter ); 661} 662 663struct str_dump_ctx 664{ 665 struct dump_ctx base; 666 char *str; 667 char *ptr; 668 int left; 669}; 670 671static void 672str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...) 673{ 674 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx; 675 676 if(sctx->left > 1) { 677 int written; 678 va_list ap; 679 va_start(ap, format); 680 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap); 681 va_end(ap); 682 683 /* Some complicated logic needed to handle the return value of 684 * vsnprintf: 685 */ 686 if (written > 0) { 687 written = MIN2(sctx->left, written); 688 sctx->ptr += written; 689 sctx->left -= written; 690 } 691 } 692} 693 694void 695tgsi_dump_str( 696 const struct tgsi_token *tokens, 697 uint flags, 698 char *str, 699 size_t size) 700{ 701 struct str_dump_ctx ctx; 702 703 ctx.base.iter.prolog = prolog; 704 ctx.base.iter.iterate_instruction = iter_instruction; 705 ctx.base.iter.iterate_declaration = iter_declaration; 706 ctx.base.iter.iterate_immediate = iter_immediate; 707 ctx.base.iter.iterate_property = iter_property; 708 ctx.base.iter.epilog = NULL; 709 710 ctx.base.instno = 0; 711 ctx.base.indent = 0; 712 ctx.base.dump_printf = &str_dump_ctx_printf; 713 ctx.base.indentation = 0; 714 715 ctx.str = str; 716 ctx.str[0] = 0; 717 ctx.ptr = str; 718 ctx.left = (int)size; 719 720 tgsi_iterate_shader( tokens, &ctx.base.iter ); 721} 722