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
[] =
135 static const char *immediate_type_names
[] =
143 tgsi_swizzle_names
[4] =
152 tgsi_texture_names
[TGSI_TEXTURE_COUNT
] =
169 const char *tgsi_property_names
[TGSI_PROPERTY_COUNT
] =
171 "GS_INPUT_PRIMITIVE",
172 "GS_OUTPUT_PRIMITIVE",
173 "GS_MAX_OUTPUT_VERTICES",
175 "FS_COORD_PIXEL_CENTER",
176 "FS_COLOR0_WRITES_ALL_CBUFS",
179 static const char *tgsi_type_names
[] =
188 const char *tgsi_primitive_names
[PIPE_PRIM_MAX
] =
201 "LINE_STRIP_ADJACENCY",
202 "TRIANGLES_ADJACENCY",
203 "TRIANGLE_STRIP_ADJACENCY"
206 const char *tgsi_fs_coord_origin_names
[2] =
212 const char *tgsi_fs_coord_pixel_center_names
[2] =
221 struct dump_ctx
*ctx
,
222 const struct tgsi_full_src_register
*src
)
224 ENM(src
->Register
.File
, tgsi_file_names
);
225 if (src
->Register
.Dimension
) {
226 if (src
->Dimension
.Indirect
) {
228 ENM( src
->DimIndirect
.File
, tgsi_file_names
);
230 SID( src
->DimIndirect
.Index
);
232 ENM( src
->DimIndirect
.SwizzleX
, tgsi_swizzle_names
);
233 if (src
->Dimension
.Index
!= 0) {
234 if (src
->Dimension
.Index
> 0)
236 SID( src
->Dimension
.Index
);
241 SID(src
->Dimension
.Index
);
245 if (src
->Register
.Indirect
) {
247 ENM( src
->Indirect
.File
, tgsi_file_names
);
249 SID( src
->Indirect
.Index
);
251 ENM( src
->Indirect
.SwizzleX
, tgsi_swizzle_names
);
252 if (src
->Register
.Index
!= 0) {
253 if (src
->Register
.Index
> 0)
255 SID( src
->Register
.Index
);
260 SID( src
->Register
.Index
);
268 struct dump_ctx
*ctx
,
269 const struct tgsi_full_dst_register
*dst
)
271 ENM(dst
->Register
.File
, tgsi_file_names
);
272 if (dst
->Register
.Dimension
) {
273 if (dst
->Dimension
.Indirect
) {
275 ENM( dst
->DimIndirect
.File
, tgsi_file_names
);
277 SID( dst
->DimIndirect
.Index
);
279 ENM( dst
->DimIndirect
.SwizzleX
, tgsi_swizzle_names
);
280 if (dst
->Dimension
.Index
!= 0) {
281 if (dst
->Dimension
.Index
> 0)
283 SID( dst
->Dimension
.Index
);
288 SID(dst
->Dimension
.Index
);
292 if (dst
->Register
.Indirect
) {
294 ENM( dst
->Indirect
.File
, tgsi_file_names
);
296 SID( dst
->Indirect
.Index
);
298 ENM( dst
->Indirect
.SwizzleX
, tgsi_swizzle_names
);
299 if (dst
->Register
.Index
!= 0) {
300 if (dst
->Register
.Index
> 0)
302 SID( dst
->Register
.Index
);
307 SID( dst
->Register
.Index
);
313 struct dump_ctx
*ctx
,
316 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
318 if (writemask
& TGSI_WRITEMASK_X
)
320 if (writemask
& TGSI_WRITEMASK_Y
)
322 if (writemask
& TGSI_WRITEMASK_Z
)
324 if (writemask
& TGSI_WRITEMASK_W
)
330 dump_imm_data(struct tgsi_iterate_context
*iter
,
331 union tgsi_immediate_data
*data
,
335 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
340 assert( num_tokens
<= 4 );
341 for (i
= 0; i
< num_tokens
; i
++) {
343 case TGSI_IMM_FLOAT32
:
344 FLT( data
[i
].Float
);
346 case TGSI_IMM_UINT32
:
356 if (i
< num_tokens
- 1)
364 struct tgsi_iterate_context
*iter
,
365 struct tgsi_full_declaration
*decl
)
367 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
369 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
370 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
374 ENM(decl
->Declaration
.File
, tgsi_file_names
);
376 /* all geometry shader inputs are two dimensional */
377 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
378 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
382 if (decl
->Declaration
.Dimension
) {
384 SID(decl
->Dim
.Index2D
);
389 SID(decl
->Range
.First
);
390 if (decl
->Range
.First
!= decl
->Range
.Last
) {
392 SID(decl
->Range
.Last
);
398 decl
->Declaration
.UsageMask
);
400 if (decl
->Declaration
.Semantic
) {
402 ENM( decl
->Semantic
.Name
, semantic_names
);
403 if (decl
->Semantic
.Index
!= 0 ||
404 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
406 UID( decl
->Semantic
.Index
);
411 if (decl
->Declaration
.File
== TGSI_FILE_RESOURCE
) {
413 ENM(decl
->Resource
.Resource
, tgsi_texture_names
);
415 if ((decl
->Resource
.ReturnTypeX
== decl
->Resource
.ReturnTypeY
) &&
416 (decl
->Resource
.ReturnTypeX
== decl
->Resource
.ReturnTypeZ
) &&
417 (decl
->Resource
.ReturnTypeX
== decl
->Resource
.ReturnTypeW
)) {
418 ENM(decl
->Resource
.ReturnTypeX
, tgsi_type_names
);
420 ENM(decl
->Resource
.ReturnTypeX
, tgsi_type_names
);
422 ENM(decl
->Resource
.ReturnTypeY
, tgsi_type_names
);
424 ENM(decl
->Resource
.ReturnTypeZ
, tgsi_type_names
);
426 ENM(decl
->Resource
.ReturnTypeW
, tgsi_type_names
);
431 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
432 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
435 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
438 if (decl
->Declaration
.Centroid
) {
442 if (decl
->Declaration
.Invariant
) {
443 TXT( ", INVARIANT" );
446 if (decl
->Declaration
.CylindricalWrap
) {
448 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
451 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
454 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
457 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
462 if (decl
->Declaration
.File
== TGSI_FILE_IMMEDIATE_ARRAY
) {
464 char range_indent
[4];
468 if (decl
->Range
.Last
< 10)
469 range_indent
[0] = '\0';
470 else if (decl
->Range
.Last
< 100) {
471 range_indent
[0] = ' ';
472 range_indent
[1] = '\0';
473 } else if (decl
->Range
.Last
< 1000) {
474 range_indent
[0] = ' ';
475 range_indent
[1] = ' ';
476 range_indent
[2] = '\0';
478 range_indent
[0] = ' ';
479 range_indent
[1] = ' ';
480 range_indent
[2] = ' ';
481 range_indent
[3] = '\0';
484 dump_imm_data(iter
, decl
->ImmediateData
.u
,
485 4, TGSI_IMM_FLOAT32
);
486 for(i
= 1; i
<= decl
->Range
.Last
; ++i
) {
487 /* indent by strlen of:
488 * "DCL IMMX[0..1] {" */
492 dump_imm_data(iter
, decl
->ImmediateData
.u
+ i
,
493 4, TGSI_IMM_FLOAT32
);
505 tgsi_dump_declaration(
506 const struct tgsi_full_declaration
*decl
)
510 ctx
.printf
= dump_ctx_printf
;
512 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
517 struct tgsi_iterate_context
*iter
,
518 struct tgsi_full_property
*prop
)
521 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
523 assert(Elements(tgsi_property_names
) == TGSI_PROPERTY_COUNT
);
526 ENM(prop
->Property
.PropertyName
, tgsi_property_names
);
528 if (prop
->Property
.NrTokens
> 1)
531 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
532 switch (prop
->Property
.PropertyName
) {
533 case TGSI_PROPERTY_GS_INPUT_PRIM
:
534 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
535 ENM(prop
->u
[i
].Data
, tgsi_primitive_names
);
537 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
538 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_origin_names
);
540 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
541 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_pixel_center_names
);
544 SID( prop
->u
[i
].Data
);
547 if (i
< prop
->Property
.NrTokens
- 2)
555 void tgsi_dump_property(
556 const struct tgsi_full_property
*prop
)
560 ctx
.printf
= dump_ctx_printf
;
562 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
567 struct tgsi_iterate_context
*iter
,
568 struct tgsi_full_immediate
*imm
)
570 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
573 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
575 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
576 imm
->Immediate
.DataType
);
585 const struct tgsi_full_immediate
*imm
)
589 ctx
.printf
= dump_ctx_printf
;
591 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
596 struct tgsi_iterate_context
*iter
,
597 struct tgsi_full_instruction
*inst
)
599 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
600 uint instno
= ctx
->instno
++;
601 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
603 boolean first_reg
= TRUE
;
608 ctx
->indent
-= info
->pre_dedent
;
609 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
611 ctx
->indent
+= info
->post_indent
;
613 if (inst
->Instruction
.Predicate
) {
616 if (inst
->Predicate
.Negate
)
620 SID( inst
->Predicate
.Index
);
623 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
624 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
625 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
626 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
628 ENM( inst
->Predicate
.SwizzleX
, tgsi_swizzle_names
);
629 ENM( inst
->Predicate
.SwizzleY
, tgsi_swizzle_names
);
630 ENM( inst
->Predicate
.SwizzleZ
, tgsi_swizzle_names
);
631 ENM( inst
->Predicate
.SwizzleW
, tgsi_swizzle_names
);
637 TXT( info
->mnemonic
);
639 switch (inst
->Instruction
.Saturate
) {
642 case TGSI_SAT_ZERO_ONE
:
645 case TGSI_SAT_MINUS_PLUS_ONE
:
652 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
653 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
659 _dump_register_dst( ctx
, dst
);
660 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
665 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
666 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
672 if (src
->Register
.Negate
)
674 if (src
->Register
.Absolute
)
677 _dump_register_src(ctx
, src
);
679 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
680 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
681 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
682 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
684 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
685 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
686 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
687 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
690 if (src
->Register
.Absolute
)
696 if (inst
->Instruction
.Texture
) {
698 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
699 for (i
= 0; i
< inst
->Texture
.NumOffsets
; i
++) {
701 ENM( inst
->TexOffsets
[i
].File
, tgsi_file_names
);
703 SID( inst
->TexOffsets
[i
].Index
);
706 ENM( inst
->TexOffsets
[i
].SwizzleX
, tgsi_swizzle_names
);
707 ENM( inst
->TexOffsets
[i
].SwizzleY
, tgsi_swizzle_names
);
708 ENM( inst
->TexOffsets
[i
].SwizzleZ
, tgsi_swizzle_names
);
712 switch (inst
->Instruction
.Opcode
) {
714 case TGSI_OPCODE_ELSE
:
715 case TGSI_OPCODE_BGNLOOP
:
716 case TGSI_OPCODE_ENDLOOP
:
717 case TGSI_OPCODE_CAL
:
719 UID( inst
->Label
.Label
);
723 /* update indentation */
724 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
725 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
726 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
727 ctx
->indentation
+= indent_spaces
;
736 tgsi_dump_instruction(
737 const struct tgsi_full_instruction
*inst
,
744 ctx
.printf
= dump_ctx_printf
;
747 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
752 struct tgsi_iterate_context
*iter
)
754 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
755 ENM( iter
->processor
.Processor
, processor_type_names
);
762 const struct tgsi_token
*tokens
,
767 ctx
.iter
.prolog
= prolog
;
768 ctx
.iter
.iterate_instruction
= iter_instruction
;
769 ctx
.iter
.iterate_declaration
= iter_declaration
;
770 ctx
.iter
.iterate_immediate
= iter_immediate
;
771 ctx
.iter
.iterate_property
= iter_property
;
772 ctx
.iter
.epilog
= NULL
;
776 ctx
.printf
= dump_ctx_printf
;
779 tgsi_iterate_shader( tokens
, &ctx
.iter
);
784 struct dump_ctx base
;
791 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
793 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
798 va_start(ap
, format
);
799 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
802 /* Some complicated logic needed to handle the return value of
806 written
= MIN2(sctx
->left
, written
);
807 sctx
->ptr
+= written
;
808 sctx
->left
-= written
;
815 const struct tgsi_token
*tokens
,
820 struct str_dump_ctx ctx
;
822 ctx
.base
.iter
.prolog
= prolog
;
823 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
824 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
825 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
826 ctx
.base
.iter
.iterate_property
= iter_property
;
827 ctx
.base
.iter
.epilog
= NULL
;
831 ctx
.base
.printf
= &str_dump_ctx_printf
;
832 ctx
.base
.indentation
= 0;
837 ctx
.left
= (int)size
;
839 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);