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
] =
107 static const char *interpolate_names
[] =
114 static const char *semantic_names
[] =
129 static const char *immediate_type_names
[] =
136 static const char *swizzle_names
[] =
144 static const char *texture_names
[] =
157 static const char *property_names
[] =
159 "GS_INPUT_PRIMITIVE",
160 "GS_OUTPUT_PRIMITIVE",
161 "GS_MAX_OUTPUT_VERTICES",
163 "FS_COORD_PIXEL_CENTER"
166 static const char *primitive_names
[] =
180 static const char *fs_coord_origin_names
[] =
186 static const char *fs_coord_pixel_center_names
[] =
195 struct dump_ctx
*ctx
,
199 ENM( file
, file_names
);
209 struct dump_ctx
*ctx
,
210 const struct tgsi_full_src_register
*src
)
212 ENM(src
->Register
.File
, file_names
);
213 if (src
->Register
.Dimension
) {
214 if (src
->Dimension
.Indirect
) {
216 ENM( src
->DimIndirect
.File
, file_names
);
218 SID( src
->DimIndirect
.Index
);
220 ENM( src
->DimIndirect
.SwizzleX
, swizzle_names
);
221 if (src
->Dimension
.Index
!= 0) {
222 if (src
->Dimension
.Index
> 0)
224 SID( src
->Dimension
.Index
);
229 SID(src
->Dimension
.Index
);
233 if (src
->Register
.Indirect
) {
235 ENM( src
->Indirect
.File
, file_names
);
237 SID( src
->Indirect
.Index
);
239 ENM( src
->Indirect
.SwizzleX
, swizzle_names
);
240 if (src
->Register
.Index
!= 0) {
241 if (src
->Register
.Index
> 0)
243 SID( src
->Register
.Index
);
248 SID( src
->Register
.Index
);
255 struct dump_ctx
*ctx
,
262 ENM( file
, file_names
);
264 ENM( ind_file
, file_names
);
268 ENM( ind_swizzle
, swizzle_names
);
279 struct dump_ctx
*ctx
,
282 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
284 if (writemask
& TGSI_WRITEMASK_X
)
286 if (writemask
& TGSI_WRITEMASK_Y
)
288 if (writemask
& TGSI_WRITEMASK_Z
)
290 if (writemask
& TGSI_WRITEMASK_W
)
297 struct tgsi_iterate_context
*iter
,
298 struct tgsi_full_declaration
*decl
)
300 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
302 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
303 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
307 ENM(decl
->Declaration
.File
, file_names
);
309 /* all geometry shader inputs are two dimensional */
310 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
311 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
315 if (decl
->Declaration
.Dimension
) {
317 SID(decl
->Dim
.Index2D
);
322 SID(decl
->Range
.First
);
323 if (decl
->Range
.First
!= decl
->Range
.Last
) {
325 SID(decl
->Range
.Last
);
331 decl
->Declaration
.UsageMask
);
333 if (decl
->Declaration
.Semantic
) {
335 ENM( decl
->Semantic
.Name
, semantic_names
);
336 if (decl
->Semantic
.Index
!= 0 ||
337 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
339 UID( decl
->Semantic
.Index
);
344 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
345 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
348 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
351 if (decl
->Declaration
.Centroid
) {
355 if (decl
->Declaration
.Invariant
) {
356 TXT( ", INVARIANT" );
359 if (decl
->Declaration
.CylindricalWrap
) {
361 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
364 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
367 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
370 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
381 tgsi_dump_declaration(
382 const struct tgsi_full_declaration
*decl
)
386 ctx
.printf
= dump_ctx_printf
;
388 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
393 struct tgsi_iterate_context
*iter
,
394 struct tgsi_full_property
*prop
)
397 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
399 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
402 ENM(prop
->Property
.PropertyName
, property_names
);
404 if (prop
->Property
.NrTokens
> 1)
407 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
408 switch (prop
->Property
.PropertyName
) {
409 case TGSI_PROPERTY_GS_INPUT_PRIM
:
410 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
411 ENM(prop
->u
[i
].Data
, primitive_names
);
413 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
414 ENM(prop
->u
[i
].Data
, fs_coord_origin_names
);
416 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
417 ENM(prop
->u
[i
].Data
, fs_coord_pixel_center_names
);
420 SID( prop
->u
[i
].Data
);
423 if (i
< prop
->Property
.NrTokens
- 2)
431 void tgsi_dump_property(
432 const struct tgsi_full_property
*prop
)
436 ctx
.printf
= dump_ctx_printf
;
438 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
443 struct tgsi_iterate_context
*iter
,
444 struct tgsi_full_immediate
*imm
)
446 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
451 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
455 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
456 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
457 switch (imm
->Immediate
.DataType
) {
458 case TGSI_IMM_FLOAT32
:
459 FLT( imm
->u
[i
].Float
);
461 case TGSI_IMM_UINT32
:
471 if (i
< imm
->Immediate
.NrTokens
- 2)
483 const struct tgsi_full_immediate
*imm
)
487 ctx
.printf
= dump_ctx_printf
;
489 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
494 struct tgsi_iterate_context
*iter
,
495 struct tgsi_full_instruction
*inst
)
497 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
498 uint instno
= ctx
->instno
++;
499 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
501 boolean first_reg
= TRUE
;
506 ctx
->indent
-= info
->pre_dedent
;
507 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
509 ctx
->indent
+= info
->post_indent
;
511 if (inst
->Instruction
.Predicate
) {
514 if (inst
->Predicate
.Negate
)
518 SID( inst
->Predicate
.Index
);
521 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
522 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
523 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
524 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
526 ENM( inst
->Predicate
.SwizzleX
, swizzle_names
);
527 ENM( inst
->Predicate
.SwizzleY
, swizzle_names
);
528 ENM( inst
->Predicate
.SwizzleZ
, swizzle_names
);
529 ENM( inst
->Predicate
.SwizzleW
, swizzle_names
);
535 TXT( info
->mnemonic
);
537 switch (inst
->Instruction
.Saturate
) {
540 case TGSI_SAT_ZERO_ONE
:
543 case TGSI_SAT_MINUS_PLUS_ONE
:
550 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
551 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
557 if (dst
->Register
.Indirect
) {
564 dst
->Indirect
.SwizzleX
);
570 dst
->Register
.Index
);
572 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
577 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
578 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
584 if (src
->Register
.Negate
)
586 if (src
->Register
.Absolute
)
589 _dump_register_src(ctx
, src
);
591 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
592 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
593 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
594 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
596 ENM( src
->Register
.SwizzleX
, swizzle_names
);
597 ENM( src
->Register
.SwizzleY
, swizzle_names
);
598 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
599 ENM( src
->Register
.SwizzleW
, swizzle_names
);
602 if (src
->Register
.Absolute
)
608 if (inst
->Instruction
.Texture
) {
610 ENM( inst
->Texture
.Texture
, texture_names
);
613 switch (inst
->Instruction
.Opcode
) {
615 case TGSI_OPCODE_ELSE
:
616 case TGSI_OPCODE_BGNLOOP
:
617 case TGSI_OPCODE_ENDLOOP
:
618 case TGSI_OPCODE_CAL
:
620 UID( inst
->Label
.Label
);
624 /* update indentation */
625 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
626 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
627 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
628 ctx
->indentation
+= indent_spaces
;
637 tgsi_dump_instruction(
638 const struct tgsi_full_instruction
*inst
,
645 ctx
.printf
= dump_ctx_printf
;
648 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
653 struct tgsi_iterate_context
*iter
)
655 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
656 ENM( iter
->processor
.Processor
, processor_type_names
);
663 const struct tgsi_token
*tokens
,
668 ctx
.iter
.prolog
= prolog
;
669 ctx
.iter
.iterate_instruction
= iter_instruction
;
670 ctx
.iter
.iterate_declaration
= iter_declaration
;
671 ctx
.iter
.iterate_immediate
= iter_immediate
;
672 ctx
.iter
.iterate_property
= iter_property
;
673 ctx
.iter
.epilog
= NULL
;
677 ctx
.printf
= dump_ctx_printf
;
680 tgsi_iterate_shader( tokens
, &ctx
.iter
);
685 struct dump_ctx base
;
692 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
694 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
699 va_start(ap
, format
);
700 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
703 /* Some complicated logic needed to handle the return value of
707 written
= MIN2(sctx
->left
, written
);
708 sctx
->ptr
+= written
;
709 sctx
->left
-= written
;
716 const struct tgsi_token
*tokens
,
721 struct str_dump_ctx ctx
;
723 ctx
.base
.iter
.prolog
= prolog
;
724 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
725 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
726 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
727 ctx
.base
.iter
.iterate_property
= iter_property
;
728 ctx
.base
.iter
.epilog
= NULL
;
732 ctx
.base
.printf
= &str_dump_ctx_printf
;
733 ctx
.base
.indentation
= 0;
738 ctx
.left
= (int)size
;
740 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);