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
[] =
93 static const char *file_names
[TGSI_FILE_COUNT
] =
109 static const char *interpolate_names
[] =
116 static const char *semantic_names
[] =
131 static const char *immediate_type_names
[] =
138 static const char *swizzle_names
[] =
146 static const char *texture_names
[] =
159 static const char *property_names
[] =
161 "GS_INPUT_PRIMITIVE",
162 "GS_OUTPUT_PRIMITIVE",
163 "GS_MAX_OUTPUT_VERTICES",
165 "FS_COORD_PIXEL_CENTER"
168 static const char *primitive_names
[] =
182 static const char *fs_coord_origin_names
[] =
188 static const char *fs_coord_pixel_center_names
[] =
197 struct dump_ctx
*ctx
,
198 const struct tgsi_full_src_register
*src
)
200 ENM(src
->Register
.File
, file_names
);
201 if (src
->Register
.Dimension
) {
202 if (src
->Dimension
.Indirect
) {
204 ENM( src
->DimIndirect
.File
, file_names
);
206 SID( src
->DimIndirect
.Index
);
208 ENM( src
->DimIndirect
.SwizzleX
, swizzle_names
);
209 if (src
->Dimension
.Index
!= 0) {
210 if (src
->Dimension
.Index
> 0)
212 SID( src
->Dimension
.Index
);
217 SID(src
->Dimension
.Index
);
221 if (src
->Register
.Indirect
) {
223 ENM( src
->Indirect
.File
, file_names
);
225 SID( src
->Indirect
.Index
);
227 ENM( src
->Indirect
.SwizzleX
, swizzle_names
);
228 if (src
->Register
.Index
!= 0) {
229 if (src
->Register
.Index
> 0)
231 SID( src
->Register
.Index
);
236 SID( src
->Register
.Index
);
244 struct dump_ctx
*ctx
,
245 const struct tgsi_full_dst_register
*dst
)
247 ENM(dst
->Register
.File
, file_names
);
248 if (dst
->Register
.Dimension
) {
249 if (dst
->Dimension
.Indirect
) {
251 ENM( dst
->DimIndirect
.File
, file_names
);
253 SID( dst
->DimIndirect
.Index
);
255 ENM( dst
->DimIndirect
.SwizzleX
, swizzle_names
);
256 if (dst
->Dimension
.Index
!= 0) {
257 if (dst
->Dimension
.Index
> 0)
259 SID( dst
->Dimension
.Index
);
264 SID(dst
->Dimension
.Index
);
268 if (dst
->Register
.Indirect
) {
270 ENM( dst
->Indirect
.File
, file_names
);
272 SID( dst
->Indirect
.Index
);
274 ENM( dst
->Indirect
.SwizzleX
, swizzle_names
);
275 if (dst
->Register
.Index
!= 0) {
276 if (dst
->Register
.Index
> 0)
278 SID( dst
->Register
.Index
);
283 SID( dst
->Register
.Index
);
289 struct dump_ctx
*ctx
,
292 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
294 if (writemask
& TGSI_WRITEMASK_X
)
296 if (writemask
& TGSI_WRITEMASK_Y
)
298 if (writemask
& TGSI_WRITEMASK_Z
)
300 if (writemask
& TGSI_WRITEMASK_W
)
306 dump_imm_data(struct tgsi_iterate_context
*iter
,
307 union tgsi_immediate_data
*data
,
311 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
316 assert( num_tokens
<= 4 );
317 for (i
= 0; i
< num_tokens
; i
++) {
319 case TGSI_IMM_FLOAT32
:
320 FLT( data
[i
].Float
);
322 case TGSI_IMM_UINT32
:
332 if (i
< num_tokens
- 1)
340 struct tgsi_iterate_context
*iter
,
341 struct tgsi_full_declaration
*decl
)
343 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
345 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
346 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
350 ENM(decl
->Declaration
.File
, file_names
);
352 /* all geometry shader inputs are two dimensional */
353 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
354 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
358 if (decl
->Declaration
.Dimension
) {
360 SID(decl
->Dim
.Index2D
);
365 SID(decl
->Range
.First
);
366 if (decl
->Range
.First
!= decl
->Range
.Last
) {
368 SID(decl
->Range
.Last
);
374 decl
->Declaration
.UsageMask
);
376 if (decl
->Declaration
.Semantic
) {
378 ENM( decl
->Semantic
.Name
, semantic_names
);
379 if (decl
->Semantic
.Index
!= 0 ||
380 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
382 UID( decl
->Semantic
.Index
);
387 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
388 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
391 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
394 if (decl
->Declaration
.Centroid
) {
398 if (decl
->Declaration
.Invariant
) {
399 TXT( ", INVARIANT" );
402 if (decl
->Declaration
.CylindricalWrap
) {
404 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
407 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
410 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
413 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
418 if (decl
->Declaration
.File
== TGSI_FILE_IMMEDIATE_ARRAY
) {
420 char range_indent
[4];
424 if (decl
->Range
.Last
< 10)
425 range_indent
[0] = '\0';
426 else if (decl
->Range
.Last
< 100) {
427 range_indent
[0] = ' ';
428 range_indent
[1] = '\0';
429 } else if (decl
->Range
.Last
< 1000) {
430 range_indent
[0] = ' ';
431 range_indent
[1] = ' ';
432 range_indent
[2] = '\0';
434 range_indent
[0] = ' ';
435 range_indent
[1] = ' ';
436 range_indent
[2] = ' ';
437 range_indent
[3] = '\0';
440 dump_imm_data(iter
, decl
->ImmediateData
.u
,
441 4, TGSI_IMM_FLOAT32
);
442 for(i
= 1; i
<= decl
->Range
.Last
; ++i
) {
443 /* indent by strlen of:
444 * "DCL IMMX[0..1] {" */
448 dump_imm_data(iter
, decl
->ImmediateData
.u
+ i
,
449 4, TGSI_IMM_FLOAT32
);
461 tgsi_dump_declaration(
462 const struct tgsi_full_declaration
*decl
)
466 ctx
.printf
= dump_ctx_printf
;
468 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
473 struct tgsi_iterate_context
*iter
,
474 struct tgsi_full_property
*prop
)
477 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
479 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
482 ENM(prop
->Property
.PropertyName
, property_names
);
484 if (prop
->Property
.NrTokens
> 1)
487 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
488 switch (prop
->Property
.PropertyName
) {
489 case TGSI_PROPERTY_GS_INPUT_PRIM
:
490 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
491 ENM(prop
->u
[i
].Data
, primitive_names
);
493 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
494 ENM(prop
->u
[i
].Data
, fs_coord_origin_names
);
496 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
497 ENM(prop
->u
[i
].Data
, fs_coord_pixel_center_names
);
500 SID( prop
->u
[i
].Data
);
503 if (i
< prop
->Property
.NrTokens
- 2)
511 void tgsi_dump_property(
512 const struct tgsi_full_property
*prop
)
516 ctx
.printf
= dump_ctx_printf
;
518 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
523 struct tgsi_iterate_context
*iter
,
524 struct tgsi_full_immediate
*imm
)
526 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
529 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
531 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
532 imm
->Immediate
.DataType
);
541 const struct tgsi_full_immediate
*imm
)
545 ctx
.printf
= dump_ctx_printf
;
547 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
552 struct tgsi_iterate_context
*iter
,
553 struct tgsi_full_instruction
*inst
)
555 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
556 uint instno
= ctx
->instno
++;
557 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
559 boolean first_reg
= TRUE
;
564 ctx
->indent
-= info
->pre_dedent
;
565 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
567 ctx
->indent
+= info
->post_indent
;
569 if (inst
->Instruction
.Predicate
) {
572 if (inst
->Predicate
.Negate
)
576 SID( inst
->Predicate
.Index
);
579 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
580 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
581 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
582 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
584 ENM( inst
->Predicate
.SwizzleX
, swizzle_names
);
585 ENM( inst
->Predicate
.SwizzleY
, swizzle_names
);
586 ENM( inst
->Predicate
.SwizzleZ
, swizzle_names
);
587 ENM( inst
->Predicate
.SwizzleW
, swizzle_names
);
593 TXT( info
->mnemonic
);
595 switch (inst
->Instruction
.Saturate
) {
598 case TGSI_SAT_ZERO_ONE
:
601 case TGSI_SAT_MINUS_PLUS_ONE
:
608 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
609 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
615 _dump_register_dst( ctx
, dst
);
616 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
621 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
622 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
628 if (src
->Register
.Negate
)
630 if (src
->Register
.Absolute
)
633 _dump_register_src(ctx
, src
);
635 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
636 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
637 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
638 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
640 ENM( src
->Register
.SwizzleX
, swizzle_names
);
641 ENM( src
->Register
.SwizzleY
, swizzle_names
);
642 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
643 ENM( src
->Register
.SwizzleW
, swizzle_names
);
646 if (src
->Register
.Absolute
)
652 if (inst
->Instruction
.Texture
) {
654 ENM( inst
->Texture
.Texture
, texture_names
);
657 switch (inst
->Instruction
.Opcode
) {
659 case TGSI_OPCODE_ELSE
:
660 case TGSI_OPCODE_BGNLOOP
:
661 case TGSI_OPCODE_ENDLOOP
:
662 case TGSI_OPCODE_CAL
:
664 UID( inst
->Label
.Label
);
668 /* update indentation */
669 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
670 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
671 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
672 ctx
->indentation
+= indent_spaces
;
681 tgsi_dump_instruction(
682 const struct tgsi_full_instruction
*inst
,
689 ctx
.printf
= dump_ctx_printf
;
692 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
697 struct tgsi_iterate_context
*iter
)
699 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
700 ENM( iter
->processor
.Processor
, processor_type_names
);
707 const struct tgsi_token
*tokens
,
712 ctx
.iter
.prolog
= prolog
;
713 ctx
.iter
.iterate_instruction
= iter_instruction
;
714 ctx
.iter
.iterate_declaration
= iter_declaration
;
715 ctx
.iter
.iterate_immediate
= iter_immediate
;
716 ctx
.iter
.iterate_property
= iter_property
;
717 ctx
.iter
.epilog
= NULL
;
721 ctx
.printf
= dump_ctx_printf
;
724 tgsi_iterate_shader( tokens
, &ctx
.iter
);
729 struct dump_ctx base
;
736 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
738 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
743 va_start(ap
, format
);
744 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
747 /* Some complicated logic needed to handle the return value of
751 written
= MIN2(sctx
->left
, written
);
752 sctx
->ptr
+= written
;
753 sctx
->left
-= written
;
760 const struct tgsi_token
*tokens
,
765 struct str_dump_ctx ctx
;
767 ctx
.base
.iter
.prolog
= prolog
;
768 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
769 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
770 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
771 ctx
.base
.iter
.iterate_property
= iter_property
;
772 ctx
.base
.iter
.epilog
= NULL
;
776 ctx
.base
.printf
= &str_dump_ctx_printf
;
777 ctx
.base
.indentation
= 0;
782 ctx
.left
= (int)size
;
784 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);