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 **************************************************************************/
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 "util/u_math.h"
33 #include "tgsi_dump.h"
34 #include "tgsi_info.h"
35 #include "tgsi_iterate.h"
36 #include "tgsi_strings.h"
39 /** Number of spaces to indent for IF/LOOP/etc */
40 static const int indent_spaces
= 3;
45 struct tgsi_iterate_context iter
;
47 boolean dump_float_as_hex
;
56 void (*dump_printf
)(struct dump_ctx
*ctx
, const char *format
, ...);
60 dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
66 vfprintf(ctx
->file
, format
, ap
);
68 _debug_vprintf(format
, ap
);
80 ctx
->dump_printf( ctx
, "%u", e
);
82 ctx
->dump_printf( ctx
, "%s", enums
[e
] );
85 #define EOL() ctx->dump_printf( ctx, "\n" )
86 #define TXT(S) ctx->dump_printf( ctx, "%s", S )
87 #define CHR(C) ctx->dump_printf( ctx, "%c", C )
88 #define UIX(I) ctx->dump_printf( ctx, "0x%x", I )
89 #define UID(I) ctx->dump_printf( ctx, "%u", I )
90 #define INSTID(I) ctx->dump_printf( ctx, "% 3u", I )
91 #define SID(I) ctx->dump_printf( ctx, "%d", I )
92 #define FLT(F) ctx->dump_printf( ctx, "%10.4f", F )
93 #define DBL(D) ctx->dump_printf( ctx, "%10.8f", D )
94 #define HFLT(F) ctx->dump_printf( ctx, "0x%08x", fui((F)) )
95 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
98 tgsi_swizzle_names
[4] =
108 struct dump_ctx
*ctx
,
109 const struct tgsi_full_src_register
*src
)
111 TXT(tgsi_file_name(src
->Register
.File
));
112 if (src
->Register
.Dimension
) {
113 if (src
->Dimension
.Indirect
) {
115 TXT(tgsi_file_name(src
->DimIndirect
.File
));
117 SID( src
->DimIndirect
.Index
);
119 ENM( src
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
120 if (src
->Dimension
.Index
!= 0) {
121 if (src
->Dimension
.Index
> 0)
123 SID( src
->Dimension
.Index
);
126 if (src
->DimIndirect
.ArrayID
) {
128 SID( src
->DimIndirect
.ArrayID
);
133 SID(src
->Dimension
.Index
);
137 if (src
->Register
.Indirect
) {
139 TXT(tgsi_file_name(src
->Indirect
.File
));
141 SID( src
->Indirect
.Index
);
143 ENM( src
->Indirect
.Swizzle
, tgsi_swizzle_names
);
144 if (src
->Register
.Index
!= 0) {
145 if (src
->Register
.Index
> 0)
147 SID( src
->Register
.Index
);
150 if (src
->Indirect
.ArrayID
) {
152 SID( src
->Indirect
.ArrayID
);
157 SID( src
->Register
.Index
);
165 struct dump_ctx
*ctx
,
166 const struct tgsi_full_dst_register
*dst
)
168 TXT(tgsi_file_name(dst
->Register
.File
));
169 if (dst
->Register
.Dimension
) {
170 if (dst
->Dimension
.Indirect
) {
172 TXT(tgsi_file_name(dst
->DimIndirect
.File
));
174 SID( dst
->DimIndirect
.Index
);
176 ENM( dst
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
177 if (dst
->Dimension
.Index
!= 0) {
178 if (dst
->Dimension
.Index
> 0)
180 SID( dst
->Dimension
.Index
);
183 if (dst
->DimIndirect
.ArrayID
) {
185 SID( dst
->DimIndirect
.ArrayID
);
190 SID(dst
->Dimension
.Index
);
194 if (dst
->Register
.Indirect
) {
196 TXT(tgsi_file_name(dst
->Indirect
.File
));
198 SID( dst
->Indirect
.Index
);
200 ENM( dst
->Indirect
.Swizzle
, tgsi_swizzle_names
);
201 if (dst
->Register
.Index
!= 0) {
202 if (dst
->Register
.Index
> 0)
204 SID( dst
->Register
.Index
);
207 if (dst
->Indirect
.ArrayID
) {
209 SID( dst
->Indirect
.ArrayID
);
214 SID( dst
->Register
.Index
);
220 struct dump_ctx
*ctx
,
223 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
225 if (writemask
& TGSI_WRITEMASK_X
)
227 if (writemask
& TGSI_WRITEMASK_Y
)
229 if (writemask
& TGSI_WRITEMASK_Z
)
231 if (writemask
& TGSI_WRITEMASK_W
)
237 dump_imm_data(struct tgsi_iterate_context
*iter
,
238 union tgsi_immediate_data
*data
,
242 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
247 assert( num_tokens
<= 4 );
248 for (i
= 0; i
< num_tokens
; i
++) {
250 case TGSI_IMM_FLOAT64
: {
252 d
.ui
= data
[i
].Uint
| (uint64_t)data
[i
+1].Uint
<< 32;
257 case TGSI_IMM_INT64
: {
259 d
.i
= data
[i
].Uint
| (uint64_t)data
[i
+1].Uint
<< 32;
264 case TGSI_IMM_UINT64
: {
266 d
.ui
= data
[i
].Uint
| (uint64_t)data
[i
+1].Uint
<< 32;
271 case TGSI_IMM_FLOAT32
:
272 if (ctx
->dump_float_as_hex
)
273 HFLT( data
[i
].Float
);
275 FLT( data
[i
].Float
);
277 case TGSI_IMM_UINT32
:
287 if (i
< num_tokens
- 1)
295 struct tgsi_iterate_context
*iter
,
296 struct tgsi_full_declaration
*decl
)
298 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
299 boolean patch
= decl
->Semantic
.Name
== TGSI_SEMANTIC_PATCH
||
300 decl
->Semantic
.Name
== TGSI_SEMANTIC_TESSINNER
||
301 decl
->Semantic
.Name
== TGSI_SEMANTIC_TESSOUTER
||
302 decl
->Semantic
.Name
== TGSI_SEMANTIC_PRIMID
;
306 TXT(tgsi_file_name(decl
->Declaration
.File
));
308 /* all geometry shader inputs and non-patch tessellation shader inputs are
311 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
312 (iter
->processor
.Processor
== PIPE_SHADER_GEOMETRY
||
314 (iter
->processor
.Processor
== PIPE_SHADER_TESS_CTRL
||
315 iter
->processor
.Processor
== PIPE_SHADER_TESS_EVAL
)))) {
319 /* all non-patch tess ctrl shader outputs are two dimensional */
320 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
322 iter
->processor
.Processor
== PIPE_SHADER_TESS_CTRL
) {
326 if (decl
->Declaration
.Dimension
) {
328 SID(decl
->Dim
.Index2D
);
333 SID(decl
->Range
.First
);
334 if (decl
->Range
.First
!= decl
->Range
.Last
) {
336 SID(decl
->Range
.Last
);
342 decl
->Declaration
.UsageMask
);
344 if (decl
->Declaration
.Array
) {
346 SID(decl
->Array
.ArrayID
);
350 if (decl
->Declaration
.Local
)
353 if (decl
->Declaration
.Semantic
) {
355 ENM( decl
->Semantic
.Name
, tgsi_semantic_names
);
356 if (decl
->Semantic
.Index
!= 0 ||
357 decl
->Semantic
.Name
== TGSI_SEMANTIC_TEXCOORD
||
358 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
360 UID( decl
->Semantic
.Index
);
364 if (decl
->Semantic
.StreamX
!= 0 || decl
->Semantic
.StreamY
!= 0 ||
365 decl
->Semantic
.StreamZ
!= 0 || decl
->Semantic
.StreamW
!= 0) {
367 UID(decl
->Semantic
.StreamX
);
369 UID(decl
->Semantic
.StreamY
);
371 UID(decl
->Semantic
.StreamZ
);
373 UID(decl
->Semantic
.StreamW
);
378 if (decl
->Declaration
.File
== TGSI_FILE_IMAGE
) {
380 ENM(decl
->Image
.Resource
, tgsi_texture_names
);
382 TXT(util_format_name(decl
->Image
.Format
));
383 if (decl
->Image
.Writable
)
389 if (decl
->Declaration
.File
== TGSI_FILE_BUFFER
) {
390 if (decl
->Declaration
.Atomic
)
394 if (decl
->Declaration
.File
== TGSI_FILE_MEMORY
) {
395 switch (decl
->Declaration
.MemType
) {
396 /* Note: ,GLOBAL is optional / the default */
397 case TGSI_MEMORY_TYPE_GLOBAL
: TXT(", GLOBAL"); break;
398 case TGSI_MEMORY_TYPE_SHARED
: TXT(", SHARED"); break;
399 case TGSI_MEMORY_TYPE_PRIVATE
: TXT(", PRIVATE"); break;
400 case TGSI_MEMORY_TYPE_INPUT
: TXT(", INPUT"); break;
404 if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER_VIEW
) {
406 ENM(decl
->SamplerView
.Resource
, tgsi_texture_names
);
408 if ((decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeY
) &&
409 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeZ
) &&
410 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeW
)) {
411 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
413 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
415 ENM(decl
->SamplerView
.ReturnTypeY
, tgsi_return_type_names
);
417 ENM(decl
->SamplerView
.ReturnTypeZ
, tgsi_return_type_names
);
419 ENM(decl
->SamplerView
.ReturnTypeW
, tgsi_return_type_names
);
423 if (decl
->Declaration
.Interpolate
) {
424 if (iter
->processor
.Processor
== PIPE_SHADER_FRAGMENT
&&
425 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
428 ENM( decl
->Interp
.Interpolate
, tgsi_interpolate_names
);
431 if (decl
->Interp
.Location
!= TGSI_INTERPOLATE_LOC_CENTER
) {
433 ENM( decl
->Interp
.Location
, tgsi_interpolate_locations
);
436 if (decl
->Interp
.CylindricalWrap
) {
438 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
441 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
444 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
447 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
453 if (decl
->Declaration
.Invariant
) {
454 TXT( ", INVARIANT" );
463 tgsi_dump_declaration(
464 const struct tgsi_full_declaration
*decl
)
467 memset(&ctx
, 0, sizeof(ctx
));
469 ctx
.dump_printf
= dump_ctx_printf
;
471 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
476 struct tgsi_iterate_context
*iter
,
477 struct tgsi_full_property
*prop
)
480 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
483 ENM(prop
->Property
.PropertyName
, tgsi_property_names
);
485 if (prop
->Property
.NrTokens
> 1)
488 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
489 switch (prop
->Property
.PropertyName
) {
490 case TGSI_PROPERTY_GS_INPUT_PRIM
:
491 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
492 ENM(prop
->u
[i
].Data
, tgsi_primitive_names
);
494 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
495 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_origin_names
);
497 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
498 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_pixel_center_names
);
500 case TGSI_PROPERTY_NEXT_SHADER
:
501 ENM(prop
->u
[i
].Data
, tgsi_processor_type_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
)
519 memset(&ctx
, 0, sizeof(ctx
));
521 ctx
.dump_printf
= dump_ctx_printf
;
523 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
528 struct tgsi_iterate_context
*iter
,
529 struct tgsi_full_immediate
*imm
)
531 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
536 ENM( imm
->Immediate
.DataType
, tgsi_immediate_type_names
);
538 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
539 imm
->Immediate
.DataType
);
548 const struct tgsi_full_immediate
*imm
)
551 memset(&ctx
, 0, sizeof(ctx
));
553 ctx
.dump_printf
= dump_ctx_printf
;
555 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
560 struct tgsi_iterate_context
*iter
,
561 struct tgsi_full_instruction
*inst
)
563 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
564 uint instno
= ctx
->instno
++;
565 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
567 boolean first_reg
= TRUE
;
572 ctx
->indent
-= info
->pre_dedent
;
573 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
575 ctx
->indent
+= info
->post_indent
;
577 if (inst
->Instruction
.Predicate
) {
580 if (inst
->Predicate
.Negate
)
584 SID( inst
->Predicate
.Index
);
587 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
588 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
589 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
590 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
592 ENM( inst
->Predicate
.SwizzleX
, tgsi_swizzle_names
);
593 ENM( inst
->Predicate
.SwizzleY
, tgsi_swizzle_names
);
594 ENM( inst
->Predicate
.SwizzleZ
, tgsi_swizzle_names
);
595 ENM( inst
->Predicate
.SwizzleW
, tgsi_swizzle_names
);
601 TXT( info
->mnemonic
);
603 if (inst
->Instruction
.Saturate
) {
607 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
608 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
614 _dump_register_dst( ctx
, dst
);
615 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
620 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
621 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
627 if (src
->Register
.Negate
)
629 if (src
->Register
.Absolute
)
632 _dump_register_src(ctx
, src
);
634 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
635 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
636 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
637 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
639 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
640 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
641 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
642 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
645 if (src
->Register
.Absolute
)
651 if (inst
->Instruction
.Texture
) {
652 if (!(inst
->Instruction
.Opcode
>= TGSI_OPCODE_SAMPLE
&&
653 inst
->Instruction
.Opcode
<= TGSI_OPCODE_GATHER4
)) {
655 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
657 for (i
= 0; i
< inst
->Texture
.NumOffsets
; i
++) {
659 TXT(tgsi_file_name(inst
->TexOffsets
[i
].File
));
661 SID( inst
->TexOffsets
[i
].Index
);
664 ENM( inst
->TexOffsets
[i
].SwizzleX
, tgsi_swizzle_names
);
665 ENM( inst
->TexOffsets
[i
].SwizzleY
, tgsi_swizzle_names
);
666 ENM( inst
->TexOffsets
[i
].SwizzleZ
, tgsi_swizzle_names
);
670 if (inst
->Instruction
.Memory
) {
671 uint32_t qualifier
= inst
->Memory
.Qualifier
;
673 int bit
= ffs(qualifier
) - 1;
674 qualifier
&= ~(1U << bit
);
676 ENM(bit
, tgsi_memory_names
);
678 if (inst
->Memory
.Texture
) {
680 ENM( inst
->Memory
.Texture
, tgsi_texture_names
);
682 if (inst
->Memory
.Format
) {
684 TXT( util_format_name(inst
->Memory
.Format
) );
688 if (inst
->Instruction
.Label
) {
689 switch (inst
->Instruction
.Opcode
) {
691 case TGSI_OPCODE_UIF
:
692 case TGSI_OPCODE_ELSE
:
693 case TGSI_OPCODE_BGNLOOP
:
694 case TGSI_OPCODE_ENDLOOP
:
695 case TGSI_OPCODE_CAL
:
696 case TGSI_OPCODE_BGNSUB
:
698 UID( inst
->Label
.Label
);
703 /* update indentation */
704 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
705 inst
->Instruction
.Opcode
== TGSI_OPCODE_UIF
||
706 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
707 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
708 ctx
->indentation
+= indent_spaces
;
717 tgsi_dump_instruction(
718 const struct tgsi_full_instruction
*inst
,
722 memset(&ctx
, 0, sizeof(ctx
));
727 ctx
.dump_printf
= dump_ctx_printf
;
731 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
736 struct tgsi_iterate_context
*iter
)
738 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
739 ENM( iter
->processor
.Processor
, tgsi_processor_type_names
);
745 init_dump_ctx(struct dump_ctx
*ctx
, uint flags
)
747 memset(ctx
, 0, sizeof(*ctx
));
749 ctx
->iter
.prolog
= prolog
;
750 ctx
->iter
.iterate_instruction
= iter_instruction
;
751 ctx
->iter
.iterate_declaration
= iter_declaration
;
752 ctx
->iter
.iterate_immediate
= iter_immediate
;
753 ctx
->iter
.iterate_property
= iter_property
;
755 if (flags
& TGSI_DUMP_FLOAT_AS_HEX
)
756 ctx
->dump_float_as_hex
= TRUE
;
760 tgsi_dump_to_file(const struct tgsi_token
*tokens
, uint flags
, FILE *file
)
763 memset(&ctx
, 0, sizeof(ctx
));
765 init_dump_ctx(&ctx
, flags
);
767 ctx
.dump_printf
= dump_ctx_printf
;
770 tgsi_iterate_shader( tokens
, &ctx
.iter
);
774 tgsi_dump(const struct tgsi_token
*tokens
, uint flags
)
776 tgsi_dump_to_file(tokens
, flags
, NULL
);
781 struct dump_ctx base
;
789 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
791 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
793 if (!sctx
->nospace
) {
796 va_start(ap
, format
);
797 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
800 /* Some complicated logic needed to handle the return value of
804 if (written
>= sctx
->left
) {
805 sctx
->nospace
= true;
806 written
= sctx
->left
;
808 sctx
->ptr
+= written
;
809 sctx
->left
-= written
;
816 const struct tgsi_token
*tokens
,
821 struct str_dump_ctx ctx
;
822 memset(&ctx
, 0, sizeof(ctx
));
824 init_dump_ctx(&ctx
.base
, flags
);
826 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
831 ctx
.left
= (int)size
;
834 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);
840 tgsi_dump_instruction_str(
841 const struct tgsi_full_instruction
*inst
,
846 struct str_dump_ctx ctx
;
847 memset(&ctx
, 0, sizeof(ctx
));
849 ctx
.base
.instno
= instno
;
850 ctx
.base
.immno
= instno
;
852 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
853 ctx
.base
.indentation
= 0;
854 ctx
.base
.file
= NULL
;
859 ctx
.left
= (int)size
;
862 iter_instruction( &ctx
.base
.iter
, (struct tgsi_full_instruction
*)inst
);