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
[] =
181 "LINE_STRIP_ADJACENCY",
182 "TRIANGLES_ADJACENCY",
183 "TRIANGLE_STRIP_ADJACENCY"
186 static const char *fs_coord_origin_names
[] =
192 static const char *fs_coord_pixel_center_names
[] =
201 struct dump_ctx
*ctx
,
202 const struct tgsi_full_src_register
*src
)
204 ENM(src
->Register
.File
, file_names
);
205 if (src
->Register
.Dimension
) {
206 if (src
->Dimension
.Indirect
) {
208 ENM( src
->DimIndirect
.File
, file_names
);
210 SID( src
->DimIndirect
.Index
);
212 ENM( src
->DimIndirect
.SwizzleX
, swizzle_names
);
213 if (src
->Dimension
.Index
!= 0) {
214 if (src
->Dimension
.Index
> 0)
216 SID( src
->Dimension
.Index
);
221 SID(src
->Dimension
.Index
);
225 if (src
->Register
.Indirect
) {
227 ENM( src
->Indirect
.File
, file_names
);
229 SID( src
->Indirect
.Index
);
231 ENM( src
->Indirect
.SwizzleX
, swizzle_names
);
232 if (src
->Register
.Index
!= 0) {
233 if (src
->Register
.Index
> 0)
235 SID( src
->Register
.Index
);
240 SID( src
->Register
.Index
);
248 struct dump_ctx
*ctx
,
249 const struct tgsi_full_dst_register
*dst
)
251 ENM(dst
->Register
.File
, file_names
);
252 if (dst
->Register
.Dimension
) {
253 if (dst
->Dimension
.Indirect
) {
255 ENM( dst
->DimIndirect
.File
, file_names
);
257 SID( dst
->DimIndirect
.Index
);
259 ENM( dst
->DimIndirect
.SwizzleX
, swizzle_names
);
260 if (dst
->Dimension
.Index
!= 0) {
261 if (dst
->Dimension
.Index
> 0)
263 SID( dst
->Dimension
.Index
);
268 SID(dst
->Dimension
.Index
);
272 if (dst
->Register
.Indirect
) {
274 ENM( dst
->Indirect
.File
, file_names
);
276 SID( dst
->Indirect
.Index
);
278 ENM( dst
->Indirect
.SwizzleX
, swizzle_names
);
279 if (dst
->Register
.Index
!= 0) {
280 if (dst
->Register
.Index
> 0)
282 SID( dst
->Register
.Index
);
287 SID( dst
->Register
.Index
);
293 struct dump_ctx
*ctx
,
296 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
298 if (writemask
& TGSI_WRITEMASK_X
)
300 if (writemask
& TGSI_WRITEMASK_Y
)
302 if (writemask
& TGSI_WRITEMASK_Z
)
304 if (writemask
& TGSI_WRITEMASK_W
)
310 dump_imm_data(struct tgsi_iterate_context
*iter
,
311 union tgsi_immediate_data
*data
,
315 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
320 assert( num_tokens
<= 4 );
321 for (i
= 0; i
< num_tokens
; i
++) {
323 case TGSI_IMM_FLOAT32
:
324 FLT( data
[i
].Float
);
326 case TGSI_IMM_UINT32
:
336 if (i
< num_tokens
- 1)
344 struct tgsi_iterate_context
*iter
,
345 struct tgsi_full_declaration
*decl
)
347 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
349 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
350 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
354 ENM(decl
->Declaration
.File
, file_names
);
356 /* all geometry shader inputs are two dimensional */
357 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
358 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
362 if (decl
->Declaration
.Dimension
) {
364 SID(decl
->Dim
.Index2D
);
369 SID(decl
->Range
.First
);
370 if (decl
->Range
.First
!= decl
->Range
.Last
) {
372 SID(decl
->Range
.Last
);
378 decl
->Declaration
.UsageMask
);
380 if (decl
->Declaration
.Semantic
) {
382 ENM( decl
->Semantic
.Name
, semantic_names
);
383 if (decl
->Semantic
.Index
!= 0 ||
384 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
386 UID( decl
->Semantic
.Index
);
391 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
392 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
395 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
398 if (decl
->Declaration
.Centroid
) {
402 if (decl
->Declaration
.Invariant
) {
403 TXT( ", INVARIANT" );
406 if (decl
->Declaration
.CylindricalWrap
) {
408 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
411 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
414 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
417 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
422 if (decl
->Declaration
.File
== TGSI_FILE_IMMEDIATE_ARRAY
) {
424 char range_indent
[4];
428 if (decl
->Range
.Last
< 10)
429 range_indent
[0] = '\0';
430 else if (decl
->Range
.Last
< 100) {
431 range_indent
[0] = ' ';
432 range_indent
[1] = '\0';
433 } else if (decl
->Range
.Last
< 1000) {
434 range_indent
[0] = ' ';
435 range_indent
[1] = ' ';
436 range_indent
[2] = '\0';
438 range_indent
[0] = ' ';
439 range_indent
[1] = ' ';
440 range_indent
[2] = ' ';
441 range_indent
[3] = '\0';
444 dump_imm_data(iter
, decl
->ImmediateData
.u
,
445 4, TGSI_IMM_FLOAT32
);
446 for(i
= 1; i
<= decl
->Range
.Last
; ++i
) {
447 /* indent by strlen of:
448 * "DCL IMMX[0..1] {" */
452 dump_imm_data(iter
, decl
->ImmediateData
.u
+ i
,
453 4, TGSI_IMM_FLOAT32
);
465 tgsi_dump_declaration(
466 const struct tgsi_full_declaration
*decl
)
470 ctx
.printf
= dump_ctx_printf
;
472 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
477 struct tgsi_iterate_context
*iter
,
478 struct tgsi_full_property
*prop
)
481 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
483 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
486 ENM(prop
->Property
.PropertyName
, property_names
);
488 if (prop
->Property
.NrTokens
> 1)
491 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
492 switch (prop
->Property
.PropertyName
) {
493 case TGSI_PROPERTY_GS_INPUT_PRIM
:
494 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
495 ENM(prop
->u
[i
].Data
, primitive_names
);
497 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
498 ENM(prop
->u
[i
].Data
, fs_coord_origin_names
);
500 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
501 ENM(prop
->u
[i
].Data
, fs_coord_pixel_center_names
);
504 SID( prop
->u
[i
].Data
);
507 if (i
< prop
->Property
.NrTokens
- 2)
515 void tgsi_dump_property(
516 const struct tgsi_full_property
*prop
)
520 ctx
.printf
= dump_ctx_printf
;
522 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
527 struct tgsi_iterate_context
*iter
,
528 struct tgsi_full_immediate
*imm
)
530 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
533 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
535 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
536 imm
->Immediate
.DataType
);
545 const struct tgsi_full_immediate
*imm
)
549 ctx
.printf
= dump_ctx_printf
;
551 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
556 struct tgsi_iterate_context
*iter
,
557 struct tgsi_full_instruction
*inst
)
559 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
560 uint instno
= ctx
->instno
++;
561 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
563 boolean first_reg
= TRUE
;
568 ctx
->indent
-= info
->pre_dedent
;
569 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
571 ctx
->indent
+= info
->post_indent
;
573 if (inst
->Instruction
.Predicate
) {
576 if (inst
->Predicate
.Negate
)
580 SID( inst
->Predicate
.Index
);
583 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
584 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
585 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
586 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
588 ENM( inst
->Predicate
.SwizzleX
, swizzle_names
);
589 ENM( inst
->Predicate
.SwizzleY
, swizzle_names
);
590 ENM( inst
->Predicate
.SwizzleZ
, swizzle_names
);
591 ENM( inst
->Predicate
.SwizzleW
, swizzle_names
);
597 TXT( info
->mnemonic
);
599 switch (inst
->Instruction
.Saturate
) {
602 case TGSI_SAT_ZERO_ONE
:
605 case TGSI_SAT_MINUS_PLUS_ONE
:
612 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
613 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
619 _dump_register_dst( ctx
, dst
);
620 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
625 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
626 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
632 if (src
->Register
.Negate
)
634 if (src
->Register
.Absolute
)
637 _dump_register_src(ctx
, src
);
639 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
640 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
641 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
642 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
644 ENM( src
->Register
.SwizzleX
, swizzle_names
);
645 ENM( src
->Register
.SwizzleY
, swizzle_names
);
646 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
647 ENM( src
->Register
.SwizzleW
, swizzle_names
);
650 if (src
->Register
.Absolute
)
656 if (inst
->Instruction
.Texture
) {
658 ENM( inst
->Texture
.Texture
, texture_names
);
661 switch (inst
->Instruction
.Opcode
) {
663 case TGSI_OPCODE_ELSE
:
664 case TGSI_OPCODE_BGNLOOP
:
665 case TGSI_OPCODE_ENDLOOP
:
666 case TGSI_OPCODE_CAL
:
668 UID( inst
->Label
.Label
);
672 /* update indentation */
673 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
674 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
675 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
676 ctx
->indentation
+= indent_spaces
;
685 tgsi_dump_instruction(
686 const struct tgsi_full_instruction
*inst
,
693 ctx
.printf
= dump_ctx_printf
;
696 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
701 struct tgsi_iterate_context
*iter
)
703 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
704 ENM( iter
->processor
.Processor
, processor_type_names
);
711 const struct tgsi_token
*tokens
,
716 ctx
.iter
.prolog
= prolog
;
717 ctx
.iter
.iterate_instruction
= iter_instruction
;
718 ctx
.iter
.iterate_declaration
= iter_declaration
;
719 ctx
.iter
.iterate_immediate
= iter_immediate
;
720 ctx
.iter
.iterate_property
= iter_property
;
721 ctx
.iter
.epilog
= NULL
;
725 ctx
.printf
= dump_ctx_printf
;
728 tgsi_iterate_shader( tokens
, &ctx
.iter
);
733 struct dump_ctx base
;
740 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
742 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
747 va_start(ap
, format
);
748 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
751 /* Some complicated logic needed to handle the return value of
755 written
= MIN2(sctx
->left
, written
);
756 sctx
->ptr
+= written
;
757 sctx
->left
-= written
;
764 const struct tgsi_token
*tokens
,
769 struct str_dump_ctx ctx
;
771 ctx
.base
.iter
.prolog
= prolog
;
772 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
773 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
774 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
775 ctx
.base
.iter
.iterate_property
= iter_property
;
776 ctx
.base
.iter
.epilog
= NULL
;
780 ctx
.base
.printf
= &str_dump_ctx_printf
;
781 ctx
.base
.indentation
= 0;
786 ctx
.left
= (int)size
;
788 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);