1 /**************************************************************************
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
37 /** Number of spaces to indent for IF/LOOP/etc */
38 static const int indent_spaces = 3;
43 struct tgsi_iterate_context iter;
50 void (*printf)(struct dump_ctx *ctx, const char *format, ...);
54 dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
59 _debug_vprintf(format, ap);
71 ctx->printf( ctx, "%u", e );
73 ctx->printf( ctx, "%s", enums[e] );
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 ) )
86 static const char *processor_type_names[] =
94 tgsi_file_names[TGSI_FILE_COUNT] =
111 static const char *interpolate_names[] =
118 static const char *semantic_names[] =
134 static const char *immediate_type_names[] =
142 tgsi_swizzle_names[4] =
151 tgsi_texture_names[TGSI_TEXTURE_COUNT] =
166 const char *tgsi_property_names[TGSI_PROPERTY_COUNT] =
168 "GS_INPUT_PRIMITIVE",
169 "GS_OUTPUT_PRIMITIVE",
170 "GS_MAX_OUTPUT_VERTICES",
172 "FS_COORD_PIXEL_CENTER",
173 "FS_COLOR0_WRITES_ALL_CBUFS",
176 static const char *tgsi_type_names[] =
185 const char *tgsi_primitive_names[PIPE_PRIM_MAX] =
198 "LINE_STRIP_ADJACENCY",
199 "TRIANGLES_ADJACENCY",
200 "TRIANGLE_STRIP_ADJACENCY"
203 const char *tgsi_fs_coord_origin_names[2] =
209 const char *tgsi_fs_coord_pixel_center_names[2] =
218 struct dump_ctx *ctx,
219 const struct tgsi_full_src_register *src )
221 ENM(src->Register.File, tgsi_file_names);
222 if (src->Register.Dimension) {
223 if (src->Dimension.Indirect) {
225 ENM( src->DimIndirect.File, tgsi_file_names );
227 SID( src->DimIndirect.Index );
229 ENM( src->DimIndirect.SwizzleX, tgsi_swizzle_names );
230 if (src->Dimension.Index != 0) {
231 if (src->Dimension.Index > 0)
233 SID( src->Dimension.Index );
238 SID(src->Dimension.Index);
242 if (src->Register.Indirect) {
244 ENM( src->Indirect.File, tgsi_file_names );
246 SID( src->Indirect.Index );
248 ENM( src->Indirect.SwizzleX, tgsi_swizzle_names );
249 if (src->Register.Index != 0) {
250 if (src->Register.Index > 0)
252 SID( src->Register.Index );
257 SID( src->Register.Index );
265 struct dump_ctx *ctx,
266 const struct tgsi_full_dst_register *dst )
268 ENM(dst->Register.File, tgsi_file_names);
269 if (dst->Register.Dimension) {
270 if (dst->Dimension.Indirect) {
272 ENM( dst->DimIndirect.File, tgsi_file_names );
274 SID( dst->DimIndirect.Index );
276 ENM( dst->DimIndirect.SwizzleX, tgsi_swizzle_names );
277 if (dst->Dimension.Index != 0) {
278 if (dst->Dimension.Index > 0)
280 SID( dst->Dimension.Index );
285 SID(dst->Dimension.Index);
289 if (dst->Register.Indirect) {
291 ENM( dst->Indirect.File, tgsi_file_names );
293 SID( dst->Indirect.Index );
295 ENM( dst->Indirect.SwizzleX, tgsi_swizzle_names );
296 if (dst->Register.Index != 0) {
297 if (dst->Register.Index > 0)
299 SID( dst->Register.Index );
304 SID( dst->Register.Index );
310 struct dump_ctx *ctx,
313 if (writemask != TGSI_WRITEMASK_XYZW) {
315 if (writemask & TGSI_WRITEMASK_X)
317 if (writemask & TGSI_WRITEMASK_Y)
319 if (writemask & TGSI_WRITEMASK_Z)
321 if (writemask & TGSI_WRITEMASK_W)
327 dump_imm_data(struct tgsi_iterate_context *iter,
328 union tgsi_immediate_data *data,
332 struct dump_ctx *ctx = (struct dump_ctx *)iter;
337 assert( num_tokens <= 4 );
338 for (i = 0; i < num_tokens; i++) {
340 case TGSI_IMM_FLOAT32:
341 FLT( data[i].Float );
343 case TGSI_IMM_UINT32:
353 if (i < num_tokens - 1)
361 struct tgsi_iterate_context *iter,
362 struct tgsi_full_declaration *decl )
364 struct dump_ctx *ctx = (struct dump_ctx *)iter;
366 assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT);
367 assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT);
371 ENM(decl->Declaration.File, tgsi_file_names);
373 /* all geometry shader inputs are two dimensional */
374 if (decl->Declaration.File == TGSI_FILE_INPUT &&
375 iter->processor.Processor == TGSI_PROCESSOR_GEOMETRY) {
379 if (decl->Declaration.Dimension) {
381 SID(decl->Dim.Index2D);
386 SID(decl->Range.First);
387 if (decl->Range.First != decl->Range.Last) {
389 SID(decl->Range.Last);
395 decl->Declaration.UsageMask );
397 if (decl->Declaration.Semantic) {
399 ENM( decl->Semantic.Name, semantic_names );
400 if (decl->Semantic.Index != 0 ||
401 decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) {
403 UID( decl->Semantic.Index );
408 if (decl->Declaration.File == TGSI_FILE_RESOURCE) {
410 ENM(decl->Resource.Resource, tgsi_texture_names);
412 if ((decl->Resource.ReturnTypeX == decl->Resource.ReturnTypeY) &&
413 (decl->Resource.ReturnTypeX == decl->Resource.ReturnTypeZ) &&
414 (decl->Resource.ReturnTypeX == decl->Resource.ReturnTypeW)) {
415 ENM(decl->Resource.ReturnTypeX, tgsi_type_names);
417 ENM(decl->Resource.ReturnTypeX, tgsi_type_names);
419 ENM(decl->Resource.ReturnTypeY, tgsi_type_names);
421 ENM(decl->Resource.ReturnTypeZ, tgsi_type_names);
423 ENM(decl->Resource.ReturnTypeW, tgsi_type_names);
428 if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT &&
429 decl->Declaration.File == TGSI_FILE_INPUT)
432 ENM( decl->Declaration.Interpolate, interpolate_names );
435 if (decl->Declaration.Centroid) {
439 if (decl->Declaration.Invariant) {
440 TXT( ", INVARIANT" );
443 if (decl->Declaration.CylindricalWrap) {
445 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_X) {
448 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Y) {
451 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Z) {
454 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_W) {
459 if (decl->Declaration.File == TGSI_FILE_IMMEDIATE_ARRAY) {
461 char range_indent[4];
465 if (decl->Range.Last < 10)
466 range_indent[0] = '\0';
467 else if (decl->Range.Last < 100) {
468 range_indent[0] = ' ';
469 range_indent[1] = '\0';
470 } else if (decl->Range.Last < 1000) {
471 range_indent[0] = ' ';
472 range_indent[1] = ' ';
473 range_indent[2] = '\0';
475 range_indent[0] = ' ';
476 range_indent[1] = ' ';
477 range_indent[2] = ' ';
478 range_indent[3] = '\0';
481 dump_imm_data(iter, decl->ImmediateData.u,
482 4, TGSI_IMM_FLOAT32);
483 for(i = 1; i <= decl->Range.Last; ++i) {
484 /* indent by strlen of:
485 * "DCL IMMX[0..1] {" */
489 dump_imm_data(iter, decl->ImmediateData.u + i,
490 4, TGSI_IMM_FLOAT32);
502 tgsi_dump_declaration(
503 const struct tgsi_full_declaration *decl )
507 ctx.printf = dump_ctx_printf;
509 iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl );
514 struct tgsi_iterate_context *iter,
515 struct tgsi_full_property *prop )
518 struct dump_ctx *ctx = (struct dump_ctx *)iter;
520 assert(Elements(tgsi_property_names) == TGSI_PROPERTY_COUNT);
523 ENM(prop->Property.PropertyName, tgsi_property_names);
525 if (prop->Property.NrTokens > 1)
528 for (i = 0; i < prop->Property.NrTokens - 1; ++i) {
529 switch (prop->Property.PropertyName) {
530 case TGSI_PROPERTY_GS_INPUT_PRIM:
531 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
532 ENM(prop->u[i].Data, tgsi_primitive_names);
534 case TGSI_PROPERTY_FS_COORD_ORIGIN:
535 ENM(prop->u[i].Data, tgsi_fs_coord_origin_names);
537 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER:
538 ENM(prop->u[i].Data, tgsi_fs_coord_pixel_center_names);
541 SID( prop->u[i].Data );
544 if (i < prop->Property.NrTokens - 2)
552 void tgsi_dump_property(
553 const struct tgsi_full_property *prop )
557 ctx.printf = dump_ctx_printf;
559 iter_property( &ctx.iter, (struct tgsi_full_property *)prop );
564 struct tgsi_iterate_context *iter,
565 struct tgsi_full_immediate *imm )
567 struct dump_ctx *ctx = (struct dump_ctx *) iter;
570 ENM( imm->Immediate.DataType, immediate_type_names );
572 dump_imm_data(iter, imm->u, imm->Immediate.NrTokens - 1,
573 imm->Immediate.DataType);
582 const struct tgsi_full_immediate *imm )
586 ctx.printf = dump_ctx_printf;
588 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm );
593 struct tgsi_iterate_context *iter,
594 struct tgsi_full_instruction *inst )
596 struct dump_ctx *ctx = (struct dump_ctx *) iter;
597 uint instno = ctx->instno++;
598 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode );
600 boolean first_reg = TRUE;
605 ctx->indent -= info->pre_dedent;
606 for(i = 0; (int)i < ctx->indent; ++i)
608 ctx->indent += info->post_indent;
610 if (inst->Instruction.Predicate) {
613 if (inst->Predicate.Negate)
617 SID( inst->Predicate.Index );
620 if (inst->Predicate.SwizzleX != TGSI_SWIZZLE_X ||
621 inst->Predicate.SwizzleY != TGSI_SWIZZLE_Y ||
622 inst->Predicate.SwizzleZ != TGSI_SWIZZLE_Z ||
623 inst->Predicate.SwizzleW != TGSI_SWIZZLE_W) {
625 ENM( inst->Predicate.SwizzleX, tgsi_swizzle_names );
626 ENM( inst->Predicate.SwizzleY, tgsi_swizzle_names );
627 ENM( inst->Predicate.SwizzleZ, tgsi_swizzle_names );
628 ENM( inst->Predicate.SwizzleW, tgsi_swizzle_names );
634 TXT( info->mnemonic );
636 switch (inst->Instruction.Saturate) {
639 case TGSI_SAT_ZERO_ONE:
642 case TGSI_SAT_MINUS_PLUS_ONE:
649 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
650 const struct tgsi_full_dst_register *dst = &inst->Dst[i];
656 _dump_register_dst( ctx, dst );
657 _dump_writemask( ctx, dst->Register.WriteMask );
662 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
663 const struct tgsi_full_src_register *src = &inst->Src[i];
669 if (src->Register.Negate)
671 if (src->Register.Absolute)
674 _dump_register_src(ctx, src);
676 if (src->Register.SwizzleX != TGSI_SWIZZLE_X ||
677 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
678 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
679 src->Register.SwizzleW != TGSI_SWIZZLE_W) {
681 ENM( src->Register.SwizzleX, tgsi_swizzle_names );
682 ENM( src->Register.SwizzleY, tgsi_swizzle_names );
683 ENM( src->Register.SwizzleZ, tgsi_swizzle_names );
684 ENM( src->Register.SwizzleW, tgsi_swizzle_names );
687 if (src->Register.Absolute)
693 if (inst->Instruction.Texture) {
695 ENM( inst->Texture.Texture, tgsi_texture_names );
698 switch (inst->Instruction.Opcode) {
700 case TGSI_OPCODE_ELSE:
701 case TGSI_OPCODE_BGNLOOP:
702 case TGSI_OPCODE_ENDLOOP:
703 case TGSI_OPCODE_CAL:
705 UID( inst->Label.Label );
709 /* update indentation */
710 if (inst->Instruction.Opcode == TGSI_OPCODE_IF ||
711 inst->Instruction.Opcode == TGSI_OPCODE_ELSE ||
712 inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) {
713 ctx->indentation += indent_spaces;
722 tgsi_dump_instruction(
723 const struct tgsi_full_instruction *inst,
730 ctx.printf = dump_ctx_printf;
733 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst );
738 struct tgsi_iterate_context *iter )
740 struct dump_ctx *ctx = (struct dump_ctx *) iter;
741 ENM( iter->processor.Processor, processor_type_names );
748 const struct tgsi_token *tokens,
753 ctx.iter.prolog = prolog;
754 ctx.iter.iterate_instruction = iter_instruction;
755 ctx.iter.iterate_declaration = iter_declaration;
756 ctx.iter.iterate_immediate = iter_immediate;
757 ctx.iter.iterate_property = iter_property;
758 ctx.iter.epilog = NULL;
762 ctx.printf = dump_ctx_printf;
765 tgsi_iterate_shader( tokens, &ctx.iter );
770 struct dump_ctx base;
777 str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
779 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx;
784 va_start(ap, format);
785 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap);
788 /* Some complicated logic needed to handle the return value of
792 written = MIN2(sctx->left, written);
793 sctx->ptr += written;
794 sctx->left -= written;
801 const struct tgsi_token *tokens,
806 struct str_dump_ctx ctx;
808 ctx.base.iter.prolog = prolog;
809 ctx.base.iter.iterate_instruction = iter_instruction;
810 ctx.base.iter.iterate_declaration = iter_declaration;
811 ctx.base.iter.iterate_immediate = iter_immediate;
812 ctx.base.iter.iterate_property = iter_property;
813 ctx.base.iter.epilog = NULL;
817 ctx.base.printf = &str_dump_ctx_printf;
818 ctx.base.indentation = 0;
823 ctx.left = (int)size;
825 tgsi_iterate_shader( tokens, &ctx.base.iter );