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_FLOAT32
:
258 if (ctx
->dump_float_as_hex
)
259 HFLT( data
[i
].Float
);
261 FLT( data
[i
].Float
);
263 case TGSI_IMM_UINT32
:
273 if (i
< num_tokens
- 1)
281 struct tgsi_iterate_context
*iter
,
282 struct tgsi_full_declaration
*decl
)
284 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
285 boolean patch
= decl
->Semantic
.Name
== TGSI_SEMANTIC_PATCH
||
286 decl
->Semantic
.Name
== TGSI_SEMANTIC_TESSINNER
||
287 decl
->Semantic
.Name
== TGSI_SEMANTIC_TESSOUTER
||
288 decl
->Semantic
.Name
== TGSI_SEMANTIC_PRIMID
;
292 TXT(tgsi_file_name(decl
->Declaration
.File
));
294 /* all geometry shader inputs and non-patch tessellation shader inputs are
297 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
298 (iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
||
300 (iter
->processor
.Processor
== TGSI_PROCESSOR_TESS_CTRL
||
301 iter
->processor
.Processor
== TGSI_PROCESSOR_TESS_EVAL
)))) {
305 /* all non-patch tess ctrl shader outputs are two dimensional */
306 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
308 iter
->processor
.Processor
== TGSI_PROCESSOR_TESS_CTRL
) {
312 if (decl
->Declaration
.Dimension
) {
314 SID(decl
->Dim
.Index2D
);
319 SID(decl
->Range
.First
);
320 if (decl
->Range
.First
!= decl
->Range
.Last
) {
322 SID(decl
->Range
.Last
);
328 decl
->Declaration
.UsageMask
);
330 if (decl
->Declaration
.Array
) {
332 SID(decl
->Array
.ArrayID
);
336 if (decl
->Declaration
.Local
)
339 if (decl
->Declaration
.Semantic
) {
341 ENM( decl
->Semantic
.Name
, tgsi_semantic_names
);
342 if (decl
->Semantic
.Index
!= 0 ||
343 decl
->Semantic
.Name
== TGSI_SEMANTIC_TEXCOORD
||
344 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
346 UID( decl
->Semantic
.Index
);
351 if (decl
->Declaration
.File
== TGSI_FILE_IMAGE
) {
353 ENM(decl
->Image
.Resource
, tgsi_texture_names
);
355 UID(decl
->Image
.Format
);
356 if (decl
->Image
.Writable
)
362 if (decl
->Declaration
.File
== TGSI_FILE_BUFFER
) {
363 if (decl
->Declaration
.Atomic
)
367 if (decl
->Declaration
.File
== TGSI_FILE_MEMORY
) {
368 if (decl
->Declaration
.Shared
)
372 if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER_VIEW
) {
374 ENM(decl
->SamplerView
.Resource
, tgsi_texture_names
);
376 if ((decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeY
) &&
377 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeZ
) &&
378 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeW
)) {
379 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
381 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
383 ENM(decl
->SamplerView
.ReturnTypeY
, tgsi_return_type_names
);
385 ENM(decl
->SamplerView
.ReturnTypeZ
, tgsi_return_type_names
);
387 ENM(decl
->SamplerView
.ReturnTypeW
, tgsi_return_type_names
);
391 if (decl
->Declaration
.Interpolate
) {
392 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
393 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
396 ENM( decl
->Interp
.Interpolate
, tgsi_interpolate_names
);
399 if (decl
->Interp
.Location
!= TGSI_INTERPOLATE_LOC_CENTER
) {
401 ENM( decl
->Interp
.Location
, tgsi_interpolate_locations
);
404 if (decl
->Interp
.CylindricalWrap
) {
406 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
409 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
412 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
415 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
421 if (decl
->Declaration
.Invariant
) {
422 TXT( ", INVARIANT" );
431 tgsi_dump_declaration(
432 const struct tgsi_full_declaration
*decl
)
436 ctx
.dump_printf
= dump_ctx_printf
;
438 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
443 struct tgsi_iterate_context
*iter
,
444 struct tgsi_full_property
*prop
)
447 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
450 ENM(prop
->Property
.PropertyName
, tgsi_property_names
);
452 if (prop
->Property
.NrTokens
> 1)
455 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
456 switch (prop
->Property
.PropertyName
) {
457 case TGSI_PROPERTY_GS_INPUT_PRIM
:
458 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
459 ENM(prop
->u
[i
].Data
, tgsi_primitive_names
);
461 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
462 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_origin_names
);
464 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
465 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_pixel_center_names
);
468 SID( prop
->u
[i
].Data
);
471 if (i
< prop
->Property
.NrTokens
- 2)
479 void tgsi_dump_property(
480 const struct tgsi_full_property
*prop
)
484 ctx
.dump_printf
= dump_ctx_printf
;
486 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
491 struct tgsi_iterate_context
*iter
,
492 struct tgsi_full_immediate
*imm
)
494 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
499 ENM( imm
->Immediate
.DataType
, tgsi_immediate_type_names
);
501 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
502 imm
->Immediate
.DataType
);
511 const struct tgsi_full_immediate
*imm
)
515 ctx
.dump_printf
= dump_ctx_printf
;
517 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
522 struct tgsi_iterate_context
*iter
,
523 struct tgsi_full_instruction
*inst
)
525 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
526 uint instno
= ctx
->instno
++;
527 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
529 boolean first_reg
= TRUE
;
534 ctx
->indent
-= info
->pre_dedent
;
535 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
537 ctx
->indent
+= info
->post_indent
;
539 if (inst
->Instruction
.Predicate
) {
542 if (inst
->Predicate
.Negate
)
546 SID( inst
->Predicate
.Index
);
549 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
550 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
551 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
552 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
554 ENM( inst
->Predicate
.SwizzleX
, tgsi_swizzle_names
);
555 ENM( inst
->Predicate
.SwizzleY
, tgsi_swizzle_names
);
556 ENM( inst
->Predicate
.SwizzleZ
, tgsi_swizzle_names
);
557 ENM( inst
->Predicate
.SwizzleW
, tgsi_swizzle_names
);
563 TXT( info
->mnemonic
);
565 if (inst
->Instruction
.Saturate
) {
569 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
570 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
576 _dump_register_dst( ctx
, dst
);
577 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
582 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
583 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
589 if (src
->Register
.Negate
)
591 if (src
->Register
.Absolute
)
594 _dump_register_src(ctx
, src
);
596 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
597 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
598 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
599 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
601 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
602 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
603 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
604 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
607 if (src
->Register
.Absolute
)
613 if (inst
->Instruction
.Texture
) {
614 if (!(inst
->Instruction
.Opcode
>= TGSI_OPCODE_SAMPLE
&&
615 inst
->Instruction
.Opcode
<= TGSI_OPCODE_GATHER4
)) {
617 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
619 for (i
= 0; i
< inst
->Texture
.NumOffsets
; i
++) {
621 TXT(tgsi_file_name(inst
->TexOffsets
[i
].File
));
623 SID( inst
->TexOffsets
[i
].Index
);
626 ENM( inst
->TexOffsets
[i
].SwizzleX
, tgsi_swizzle_names
);
627 ENM( inst
->TexOffsets
[i
].SwizzleY
, tgsi_swizzle_names
);
628 ENM( inst
->TexOffsets
[i
].SwizzleZ
, tgsi_swizzle_names
);
632 if (inst
->Instruction
.Memory
) {
633 uint32_t qualifier
= inst
->Memory
.Qualifier
;
635 int bit
= ffs(qualifier
) - 1;
636 qualifier
&= ~(1U << bit
);
638 ENM(bit
, tgsi_memory_names
);
642 switch (inst
->Instruction
.Opcode
) {
644 case TGSI_OPCODE_UIF
:
645 case TGSI_OPCODE_ELSE
:
646 case TGSI_OPCODE_BGNLOOP
:
647 case TGSI_OPCODE_ENDLOOP
:
648 case TGSI_OPCODE_CAL
:
649 case TGSI_OPCODE_BGNSUB
:
651 UID( inst
->Label
.Label
);
655 /* update indentation */
656 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
657 inst
->Instruction
.Opcode
== TGSI_OPCODE_UIF
||
658 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
659 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
660 ctx
->indentation
+= indent_spaces
;
669 tgsi_dump_instruction(
670 const struct tgsi_full_instruction
*inst
,
678 ctx
.dump_printf
= dump_ctx_printf
;
682 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
687 struct tgsi_iterate_context
*iter
)
689 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
690 ENM( iter
->processor
.Processor
, tgsi_processor_type_names
);
696 tgsi_dump_to_file(const struct tgsi_token
*tokens
, uint flags
, FILE *file
)
700 ctx
.iter
.prolog
= prolog
;
701 ctx
.iter
.iterate_instruction
= iter_instruction
;
702 ctx
.iter
.iterate_declaration
= iter_declaration
;
703 ctx
.iter
.iterate_immediate
= iter_immediate
;
704 ctx
.iter
.iterate_property
= iter_property
;
705 ctx
.iter
.epilog
= NULL
;
710 ctx
.dump_printf
= dump_ctx_printf
;
714 if (flags
& TGSI_DUMP_FLOAT_AS_HEX
)
715 ctx
.dump_float_as_hex
= TRUE
;
717 ctx
.dump_float_as_hex
= FALSE
;
719 tgsi_iterate_shader( tokens
, &ctx
.iter
);
723 tgsi_dump(const struct tgsi_token
*tokens
, uint flags
)
725 tgsi_dump_to_file(tokens
, flags
, NULL
);
730 struct dump_ctx base
;
738 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
740 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
745 va_start(ap
, format
);
746 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
749 /* Some complicated logic needed to handle the return value of
753 written
= MIN2(sctx
->left
, written
);
754 sctx
->ptr
+= written
;
755 sctx
->left
-= written
;
758 sctx
->nospace
= true;
763 const struct tgsi_token
*tokens
,
768 struct str_dump_ctx ctx
;
770 ctx
.base
.iter
.prolog
= prolog
;
771 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
772 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
773 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
774 ctx
.base
.iter
.iterate_property
= iter_property
;
775 ctx
.base
.iter
.epilog
= NULL
;
780 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
781 ctx
.base
.indentation
= 0;
782 ctx
.base
.file
= NULL
;
787 ctx
.left
= (int)size
;
790 if (flags
& TGSI_DUMP_FLOAT_AS_HEX
)
791 ctx
.base
.dump_float_as_hex
= TRUE
;
793 ctx
.base
.dump_float_as_hex
= FALSE
;
795 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);
801 tgsi_dump_instruction_str(
802 const struct tgsi_full_instruction
*inst
,
807 struct str_dump_ctx ctx
;
809 ctx
.base
.instno
= instno
;
810 ctx
.base
.immno
= instno
;
812 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
813 ctx
.base
.indentation
= 0;
814 ctx
.base
.file
= NULL
;
819 ctx
.left
= (int)size
;
822 iter_instruction( &ctx
.base
.iter
, (struct tgsi_full_instruction
*)inst
);