1 /**************************************************************************
3 * Copyright 2007-2008 VMware, Inc.
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 VMWARE 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 **************************************************************************/
30 #include "util/u_debug.h"
31 #include "util/u_string.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "util/u_math.h"
35 #include "tgsi_dump.h"
36 #include "tgsi_info.h"
37 #include "tgsi_iterate.h"
38 #include "tgsi_strings.h"
41 /** Number of spaces to indent for IF/LOOP/etc */
42 static const int indent_spaces
= 3;
47 struct tgsi_iterate_context iter
;
49 boolean dump_float_as_hex
;
58 void (*dump_printf
)(struct dump_ctx
*ctx
, const char *format
, ...);
62 dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
68 vfprintf(ctx
->file
, format
, ap
);
70 _debug_vprintf(format
, ap
);
82 ctx
->dump_printf( ctx
, "%u", e
);
84 ctx
->dump_printf( ctx
, "%s", enums
[e
] );
87 #define EOL() ctx->dump_printf( ctx, "\n" )
88 #define TXT(S) ctx->dump_printf( ctx, "%s", S )
89 #define CHR(C) ctx->dump_printf( ctx, "%c", C )
90 #define UIX(I) ctx->dump_printf( ctx, "0x%x", I )
91 #define UID(I) ctx->dump_printf( ctx, "%u", I )
92 #define SI64D(I) ctx->dump_printf( ctx, "%"PRId64, I )
93 #define UI64D(I) ctx->dump_printf( ctx, "%"PRIu64, I )
94 #define INSTID(I) ctx->dump_printf( ctx, "% 3u", I )
95 #define SID(I) ctx->dump_printf( ctx, "%d", I )
96 #define FLT(F) ctx->dump_printf( ctx, "%10.4f", F )
97 #define DBL(D) ctx->dump_printf( ctx, "%10.8f", D )
98 #define HFLT(F) ctx->dump_printf( ctx, "0x%08x", fui((F)) )
99 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
102 tgsi_swizzle_names
[4] =
112 struct dump_ctx
*ctx
,
113 const struct tgsi_full_src_register
*src
)
115 TXT(tgsi_file_name(src
->Register
.File
));
116 if (src
->Register
.Dimension
) {
117 if (src
->Dimension
.Indirect
) {
119 TXT(tgsi_file_name(src
->DimIndirect
.File
));
121 SID( src
->DimIndirect
.Index
);
123 ENM( src
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
124 if (src
->Dimension
.Index
!= 0) {
125 if (src
->Dimension
.Index
> 0)
127 SID( src
->Dimension
.Index
);
130 if (src
->DimIndirect
.ArrayID
) {
132 SID( src
->DimIndirect
.ArrayID
);
137 SID(src
->Dimension
.Index
);
141 if (src
->Register
.Indirect
) {
143 TXT(tgsi_file_name(src
->Indirect
.File
));
145 SID( src
->Indirect
.Index
);
147 ENM( src
->Indirect
.Swizzle
, tgsi_swizzle_names
);
148 if (src
->Register
.Index
!= 0) {
149 if (src
->Register
.Index
> 0)
151 SID( src
->Register
.Index
);
154 if (src
->Indirect
.ArrayID
) {
156 SID( src
->Indirect
.ArrayID
);
161 SID( src
->Register
.Index
);
169 struct dump_ctx
*ctx
,
170 const struct tgsi_full_dst_register
*dst
)
172 TXT(tgsi_file_name(dst
->Register
.File
));
173 if (dst
->Register
.Dimension
) {
174 if (dst
->Dimension
.Indirect
) {
176 TXT(tgsi_file_name(dst
->DimIndirect
.File
));
178 SID( dst
->DimIndirect
.Index
);
180 ENM( dst
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
181 if (dst
->Dimension
.Index
!= 0) {
182 if (dst
->Dimension
.Index
> 0)
184 SID( dst
->Dimension
.Index
);
187 if (dst
->DimIndirect
.ArrayID
) {
189 SID( dst
->DimIndirect
.ArrayID
);
194 SID(dst
->Dimension
.Index
);
198 if (dst
->Register
.Indirect
) {
200 TXT(tgsi_file_name(dst
->Indirect
.File
));
202 SID( dst
->Indirect
.Index
);
204 ENM( dst
->Indirect
.Swizzle
, tgsi_swizzle_names
);
205 if (dst
->Register
.Index
!= 0) {
206 if (dst
->Register
.Index
> 0)
208 SID( dst
->Register
.Index
);
211 if (dst
->Indirect
.ArrayID
) {
213 SID( dst
->Indirect
.ArrayID
);
218 SID( dst
->Register
.Index
);
224 struct dump_ctx
*ctx
,
227 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
229 if (writemask
& TGSI_WRITEMASK_X
)
231 if (writemask
& TGSI_WRITEMASK_Y
)
233 if (writemask
& TGSI_WRITEMASK_Z
)
235 if (writemask
& TGSI_WRITEMASK_W
)
241 dump_imm_data(struct tgsi_iterate_context
*iter
,
242 union tgsi_immediate_data
*data
,
246 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
251 assert( num_tokens
<= 4 );
252 for (i
= 0; i
< num_tokens
; i
++) {
254 case TGSI_IMM_FLOAT64
: {
256 d
.ui
= data
[i
].Uint
| (uint64_t)data
[i
+1].Uint
<< 32;
261 case TGSI_IMM_INT64
: {
263 d
.i
= data
[i
].Uint
| (uint64_t)data
[i
+1].Uint
<< 32;
268 case TGSI_IMM_UINT64
: {
270 d
.ui
= data
[i
].Uint
| (uint64_t)data
[i
+1].Uint
<< 32;
275 case TGSI_IMM_FLOAT32
:
276 if (ctx
->dump_float_as_hex
)
277 HFLT( data
[i
].Float
);
279 FLT( data
[i
].Float
);
281 case TGSI_IMM_UINT32
:
291 if (i
< num_tokens
- 1)
299 struct tgsi_iterate_context
*iter
,
300 struct tgsi_full_declaration
*decl
)
302 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
303 boolean patch
= decl
->Semantic
.Name
== TGSI_SEMANTIC_PATCH
||
304 decl
->Semantic
.Name
== TGSI_SEMANTIC_TESSINNER
||
305 decl
->Semantic
.Name
== TGSI_SEMANTIC_TESSOUTER
||
306 decl
->Semantic
.Name
== TGSI_SEMANTIC_PRIMID
;
310 TXT(tgsi_file_name(decl
->Declaration
.File
));
312 /* all geometry shader inputs and non-patch tessellation shader inputs are
315 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
316 (iter
->processor
.Processor
== PIPE_SHADER_GEOMETRY
||
318 (iter
->processor
.Processor
== PIPE_SHADER_TESS_CTRL
||
319 iter
->processor
.Processor
== PIPE_SHADER_TESS_EVAL
)))) {
323 /* all non-patch tess ctrl shader outputs are two dimensional */
324 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
326 iter
->processor
.Processor
== PIPE_SHADER_TESS_CTRL
) {
330 if (decl
->Declaration
.Dimension
) {
332 SID(decl
->Dim
.Index2D
);
337 SID(decl
->Range
.First
);
338 if (decl
->Range
.First
!= decl
->Range
.Last
) {
340 SID(decl
->Range
.Last
);
346 decl
->Declaration
.UsageMask
);
348 if (decl
->Declaration
.Array
) {
350 SID(decl
->Array
.ArrayID
);
354 if (decl
->Declaration
.Local
)
357 if (decl
->Declaration
.Semantic
) {
359 ENM( decl
->Semantic
.Name
, tgsi_semantic_names
);
360 if (decl
->Semantic
.Index
!= 0 ||
361 decl
->Semantic
.Name
== TGSI_SEMANTIC_TEXCOORD
||
362 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
364 UID( decl
->Semantic
.Index
);
368 if (decl
->Semantic
.StreamX
!= 0 || decl
->Semantic
.StreamY
!= 0 ||
369 decl
->Semantic
.StreamZ
!= 0 || decl
->Semantic
.StreamW
!= 0) {
371 UID(decl
->Semantic
.StreamX
);
373 UID(decl
->Semantic
.StreamY
);
375 UID(decl
->Semantic
.StreamZ
);
377 UID(decl
->Semantic
.StreamW
);
382 if (decl
->Declaration
.File
== TGSI_FILE_IMAGE
) {
384 ENM(decl
->Image
.Resource
, tgsi_texture_names
);
386 TXT(util_format_name(decl
->Image
.Format
));
387 if (decl
->Image
.Writable
)
393 if (decl
->Declaration
.File
== TGSI_FILE_BUFFER
) {
394 if (decl
->Declaration
.Atomic
)
398 if (decl
->Declaration
.File
== TGSI_FILE_MEMORY
) {
399 switch (decl
->Declaration
.MemType
) {
400 /* Note: ,GLOBAL is optional / the default */
401 case TGSI_MEMORY_TYPE_GLOBAL
: TXT(", GLOBAL"); break;
402 case TGSI_MEMORY_TYPE_SHARED
: TXT(", SHARED"); break;
403 case TGSI_MEMORY_TYPE_PRIVATE
: TXT(", PRIVATE"); break;
404 case TGSI_MEMORY_TYPE_INPUT
: TXT(", INPUT"); break;
408 if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER_VIEW
) {
410 ENM(decl
->SamplerView
.Resource
, tgsi_texture_names
);
412 if ((decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeY
) &&
413 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeZ
) &&
414 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeW
)) {
415 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
417 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
419 ENM(decl
->SamplerView
.ReturnTypeY
, tgsi_return_type_names
);
421 ENM(decl
->SamplerView
.ReturnTypeZ
, tgsi_return_type_names
);
423 ENM(decl
->SamplerView
.ReturnTypeW
, tgsi_return_type_names
);
427 if (decl
->Declaration
.Interpolate
) {
428 if (iter
->processor
.Processor
== PIPE_SHADER_FRAGMENT
&&
429 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
432 ENM( decl
->Interp
.Interpolate
, tgsi_interpolate_names
);
435 if (decl
->Interp
.Location
!= TGSI_INTERPOLATE_LOC_CENTER
) {
437 ENM( decl
->Interp
.Location
, tgsi_interpolate_locations
);
440 if (decl
->Interp
.CylindricalWrap
) {
442 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
445 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
448 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
451 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
457 if (decl
->Declaration
.Invariant
) {
458 TXT( ", INVARIANT" );
467 tgsi_dump_declaration(
468 const struct tgsi_full_declaration
*decl
)
471 memset(&ctx
, 0, sizeof(ctx
));
473 ctx
.dump_printf
= dump_ctx_printf
;
475 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
480 struct tgsi_iterate_context
*iter
,
481 struct tgsi_full_property
*prop
)
484 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
487 ENM(prop
->Property
.PropertyName
, tgsi_property_names
);
489 if (prop
->Property
.NrTokens
> 1)
492 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
493 switch (prop
->Property
.PropertyName
) {
494 case TGSI_PROPERTY_GS_INPUT_PRIM
:
495 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
496 ENM(prop
->u
[i
].Data
, tgsi_primitive_names
);
498 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
499 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_origin_names
);
501 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
502 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_pixel_center_names
);
504 case TGSI_PROPERTY_NEXT_SHADER
:
505 ENM(prop
->u
[i
].Data
, tgsi_processor_type_names
);
508 SID( prop
->u
[i
].Data
);
511 if (i
< prop
->Property
.NrTokens
- 2)
519 void tgsi_dump_property(
520 const struct tgsi_full_property
*prop
)
523 memset(&ctx
, 0, sizeof(ctx
));
525 ctx
.dump_printf
= dump_ctx_printf
;
527 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
532 struct tgsi_iterate_context
*iter
,
533 struct tgsi_full_immediate
*imm
)
535 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
540 ENM( imm
->Immediate
.DataType
, tgsi_immediate_type_names
);
542 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
543 imm
->Immediate
.DataType
);
552 const struct tgsi_full_immediate
*imm
)
555 memset(&ctx
, 0, sizeof(ctx
));
557 ctx
.dump_printf
= dump_ctx_printf
;
559 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
564 struct tgsi_iterate_context
*iter
,
565 struct tgsi_full_instruction
*inst
)
567 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
568 uint instno
= ctx
->instno
++;
569 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
571 boolean first_reg
= TRUE
;
576 ctx
->indent
-= info
->pre_dedent
;
577 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
579 ctx
->indent
+= info
->post_indent
;
581 TXT( tgsi_get_opcode_name(inst
->Instruction
.Opcode
) );
583 if (inst
->Instruction
.Saturate
) {
587 if (inst
->Instruction
.Precise
) {
591 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
592 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
598 _dump_register_dst( ctx
, dst
);
599 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
604 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
605 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
611 if (src
->Register
.Negate
)
613 if (src
->Register
.Absolute
)
616 _dump_register_src(ctx
, src
);
618 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
619 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
620 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
621 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
623 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
624 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
625 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
626 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
629 if (src
->Register
.Absolute
)
635 if (inst
->Instruction
.Texture
) {
636 if (!(inst
->Instruction
.Opcode
>= TGSI_OPCODE_SAMPLE
&&
637 inst
->Instruction
.Opcode
<= TGSI_OPCODE_GATHER4
)) {
639 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
641 for (i
= 0; i
< inst
->Texture
.NumOffsets
; i
++) {
643 TXT(tgsi_file_name(inst
->TexOffsets
[i
].File
));
645 SID( inst
->TexOffsets
[i
].Index
);
648 ENM( inst
->TexOffsets
[i
].SwizzleX
, tgsi_swizzle_names
);
649 ENM( inst
->TexOffsets
[i
].SwizzleY
, tgsi_swizzle_names
);
650 ENM( inst
->TexOffsets
[i
].SwizzleZ
, tgsi_swizzle_names
);
654 if (inst
->Instruction
.Memory
) {
655 uint32_t qualifier
= inst
->Memory
.Qualifier
;
657 int bit
= ffs(qualifier
) - 1;
658 qualifier
&= ~(1U << bit
);
660 ENM(bit
, tgsi_memory_names
);
662 if (inst
->Memory
.Texture
) {
664 ENM( inst
->Memory
.Texture
, tgsi_texture_names
);
666 if (inst
->Memory
.Format
) {
668 TXT( util_format_name(inst
->Memory
.Format
) );
672 if (inst
->Instruction
.Label
) {
673 switch (inst
->Instruction
.Opcode
) {
675 case TGSI_OPCODE_UIF
:
676 case TGSI_OPCODE_ELSE
:
677 case TGSI_OPCODE_BGNLOOP
:
678 case TGSI_OPCODE_ENDLOOP
:
679 case TGSI_OPCODE_CAL
:
680 case TGSI_OPCODE_BGNSUB
:
682 UID( inst
->Label
.Label
);
687 /* update indentation */
688 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
689 inst
->Instruction
.Opcode
== TGSI_OPCODE_UIF
||
690 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
691 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
692 ctx
->indentation
+= indent_spaces
;
701 tgsi_dump_instruction(
702 const struct tgsi_full_instruction
*inst
,
706 memset(&ctx
, 0, sizeof(ctx
));
711 ctx
.dump_printf
= dump_ctx_printf
;
715 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
720 struct tgsi_iterate_context
*iter
)
722 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
723 ENM( iter
->processor
.Processor
, tgsi_processor_type_names
);
729 init_dump_ctx(struct dump_ctx
*ctx
, uint flags
)
731 memset(ctx
, 0, sizeof(*ctx
));
733 ctx
->iter
.prolog
= prolog
;
734 ctx
->iter
.iterate_instruction
= iter_instruction
;
735 ctx
->iter
.iterate_declaration
= iter_declaration
;
736 ctx
->iter
.iterate_immediate
= iter_immediate
;
737 ctx
->iter
.iterate_property
= iter_property
;
739 if (flags
& TGSI_DUMP_FLOAT_AS_HEX
)
740 ctx
->dump_float_as_hex
= TRUE
;
744 tgsi_dump_to_file(const struct tgsi_token
*tokens
, uint flags
, FILE *file
)
747 memset(&ctx
, 0, sizeof(ctx
));
749 init_dump_ctx(&ctx
, flags
);
751 ctx
.dump_printf
= dump_ctx_printf
;
754 tgsi_iterate_shader( tokens
, &ctx
.iter
);
758 tgsi_dump(const struct tgsi_token
*tokens
, uint flags
)
760 tgsi_dump_to_file(tokens
, flags
, NULL
);
765 struct dump_ctx base
;
773 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
775 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
777 if (!sctx
->nospace
) {
780 va_start(ap
, format
);
781 written
= vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
784 /* Some complicated logic needed to handle the return value of
788 if (written
>= sctx
->left
) {
789 sctx
->nospace
= true;
790 written
= sctx
->left
;
792 sctx
->ptr
+= written
;
793 sctx
->left
-= written
;
800 const struct tgsi_token
*tokens
,
805 struct str_dump_ctx ctx
;
806 memset(&ctx
, 0, sizeof(ctx
));
808 init_dump_ctx(&ctx
.base
, flags
);
810 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
815 ctx
.left
= (int)size
;
818 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);
824 tgsi_dump_instruction_str(
825 const struct tgsi_full_instruction
*inst
,
830 struct str_dump_ctx ctx
;
831 memset(&ctx
, 0, sizeof(ctx
));
833 ctx
.base
.instno
= instno
;
834 ctx
.base
.immno
= instno
;
836 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
837 ctx
.base
.indentation
= 0;
838 ctx
.base
.file
= NULL
;
843 ctx
.left
= (int)size
;
846 iter_instruction( &ctx
.base
.iter
, (struct tgsi_full_instruction
*)inst
);