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 "tgsi_dump.h"
33 #include "tgsi_info.h"
34 #include "tgsi_iterate.h"
35 #include "tgsi_strings.h"
38 /** Number of spaces to indent for IF/LOOP/etc */
39 static const int indent_spaces
= 3;
44 struct tgsi_iterate_context iter
;
53 void (*dump_printf
)(struct dump_ctx
*ctx
, const char *format
, ...);
57 dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
63 vfprintf(ctx
->file
, format
, ap
);
65 _debug_vprintf(format
, ap
);
77 ctx
->dump_printf( ctx
, "%u", e
);
79 ctx
->dump_printf( ctx
, "%s", enums
[e
] );
82 #define EOL() ctx->dump_printf( ctx, "\n" )
83 #define TXT(S) ctx->dump_printf( ctx, "%s", S )
84 #define CHR(C) ctx->dump_printf( ctx, "%c", C )
85 #define UIX(I) ctx->dump_printf( ctx, "0x%x", I )
86 #define UID(I) ctx->dump_printf( ctx, "%u", I )
87 #define INSTID(I) ctx->dump_printf( ctx, "% 3u", I )
88 #define SID(I) ctx->dump_printf( ctx, "%d", I )
89 #define FLT(F) ctx->dump_printf( ctx, "%10.4f", F )
90 #define DBL(D) ctx->dump_printf( ctx, "%10.8f", D )
91 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
94 tgsi_swizzle_names
[4] =
104 struct dump_ctx
*ctx
,
105 const struct tgsi_full_src_register
*src
)
107 TXT(tgsi_file_name(src
->Register
.File
));
108 if (src
->Register
.Dimension
) {
109 if (src
->Dimension
.Indirect
) {
111 TXT(tgsi_file_name(src
->DimIndirect
.File
));
113 SID( src
->DimIndirect
.Index
);
115 ENM( src
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
116 if (src
->Dimension
.Index
!= 0) {
117 if (src
->Dimension
.Index
> 0)
119 SID( src
->Dimension
.Index
);
122 if (src
->DimIndirect
.ArrayID
) {
124 SID( src
->DimIndirect
.ArrayID
);
129 SID(src
->Dimension
.Index
);
133 if (src
->Register
.Indirect
) {
135 TXT(tgsi_file_name(src
->Indirect
.File
));
137 SID( src
->Indirect
.Index
);
139 ENM( src
->Indirect
.Swizzle
, tgsi_swizzle_names
);
140 if (src
->Register
.Index
!= 0) {
141 if (src
->Register
.Index
> 0)
143 SID( src
->Register
.Index
);
146 if (src
->Indirect
.ArrayID
) {
148 SID( src
->Indirect
.ArrayID
);
153 SID( src
->Register
.Index
);
161 struct dump_ctx
*ctx
,
162 const struct tgsi_full_dst_register
*dst
)
164 TXT(tgsi_file_name(dst
->Register
.File
));
165 if (dst
->Register
.Dimension
) {
166 if (dst
->Dimension
.Indirect
) {
168 TXT(tgsi_file_name(dst
->DimIndirect
.File
));
170 SID( dst
->DimIndirect
.Index
);
172 ENM( dst
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
173 if (dst
->Dimension
.Index
!= 0) {
174 if (dst
->Dimension
.Index
> 0)
176 SID( dst
->Dimension
.Index
);
179 if (dst
->DimIndirect
.ArrayID
) {
181 SID( dst
->DimIndirect
.ArrayID
);
186 SID(dst
->Dimension
.Index
);
190 if (dst
->Register
.Indirect
) {
192 TXT(tgsi_file_name(dst
->Indirect
.File
));
194 SID( dst
->Indirect
.Index
);
196 ENM( dst
->Indirect
.Swizzle
, tgsi_swizzle_names
);
197 if (dst
->Register
.Index
!= 0) {
198 if (dst
->Register
.Index
> 0)
200 SID( dst
->Register
.Index
);
203 if (dst
->Indirect
.ArrayID
) {
205 SID( dst
->Indirect
.ArrayID
);
210 SID( dst
->Register
.Index
);
216 struct dump_ctx
*ctx
,
219 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
221 if (writemask
& TGSI_WRITEMASK_X
)
223 if (writemask
& TGSI_WRITEMASK_Y
)
225 if (writemask
& TGSI_WRITEMASK_Z
)
227 if (writemask
& TGSI_WRITEMASK_W
)
233 dump_imm_data(struct tgsi_iterate_context
*iter
,
234 union tgsi_immediate_data
*data
,
238 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
243 assert( num_tokens
<= 4 );
244 for (i
= 0; i
< num_tokens
; i
++) {
246 case TGSI_IMM_FLOAT64
: {
248 d
.ui
= data
[i
].Uint
| (uint64_t)data
[i
+1].Uint
<< 32;
253 case TGSI_IMM_FLOAT32
:
254 FLT( data
[i
].Float
);
256 case TGSI_IMM_UINT32
:
266 if (i
< num_tokens
- 1)
274 struct tgsi_iterate_context
*iter
,
275 struct tgsi_full_declaration
*decl
)
277 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
278 boolean patch
= decl
->Semantic
.Name
== TGSI_SEMANTIC_PATCH
||
279 decl
->Semantic
.Name
== TGSI_SEMANTIC_TESSINNER
||
280 decl
->Semantic
.Name
== TGSI_SEMANTIC_TESSOUTER
||
281 decl
->Semantic
.Name
== TGSI_SEMANTIC_PRIMID
;
285 TXT(tgsi_file_name(decl
->Declaration
.File
));
287 /* all geometry shader inputs and non-patch tessellation shader inputs are
290 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
291 (iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
||
293 (iter
->processor
.Processor
== TGSI_PROCESSOR_TESS_CTRL
||
294 iter
->processor
.Processor
== TGSI_PROCESSOR_TESS_EVAL
)))) {
298 /* all non-patch tess ctrl shader outputs are two dimensional */
299 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
&&
301 iter
->processor
.Processor
== TGSI_PROCESSOR_TESS_CTRL
) {
305 if (decl
->Declaration
.Dimension
) {
307 SID(decl
->Dim
.Index2D
);
312 SID(decl
->Range
.First
);
313 if (decl
->Range
.First
!= decl
->Range
.Last
) {
315 SID(decl
->Range
.Last
);
321 decl
->Declaration
.UsageMask
);
323 if (decl
->Declaration
.Array
) {
325 SID(decl
->Array
.ArrayID
);
329 if (decl
->Declaration
.Local
)
332 if (decl
->Declaration
.Semantic
) {
334 ENM( decl
->Semantic
.Name
, tgsi_semantic_names
);
335 if (decl
->Semantic
.Index
!= 0 ||
336 decl
->Semantic
.Name
== TGSI_SEMANTIC_TEXCOORD
||
337 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
339 UID( decl
->Semantic
.Index
);
344 if (decl
->Declaration
.File
== TGSI_FILE_RESOURCE
) {
346 ENM(decl
->Resource
.Resource
, tgsi_texture_names
);
347 if (decl
->Resource
.Writable
)
349 if (decl
->Resource
.Raw
)
353 if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER_VIEW
) {
355 ENM(decl
->SamplerView
.Resource
, tgsi_texture_names
);
357 if ((decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeY
) &&
358 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeZ
) &&
359 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeW
)) {
360 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
362 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
364 ENM(decl
->SamplerView
.ReturnTypeY
, tgsi_return_type_names
);
366 ENM(decl
->SamplerView
.ReturnTypeZ
, tgsi_return_type_names
);
368 ENM(decl
->SamplerView
.ReturnTypeW
, tgsi_return_type_names
);
372 if (decl
->Declaration
.Interpolate
) {
373 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
374 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
377 ENM( decl
->Interp
.Interpolate
, tgsi_interpolate_names
);
380 if (decl
->Interp
.Location
!= TGSI_INTERPOLATE_LOC_CENTER
) {
382 ENM( decl
->Interp
.Location
, tgsi_interpolate_locations
);
385 if (decl
->Interp
.CylindricalWrap
) {
387 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
390 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
393 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
396 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
402 if (decl
->Declaration
.Invariant
) {
403 TXT( ", INVARIANT" );
412 tgsi_dump_declaration(
413 const struct tgsi_full_declaration
*decl
)
417 ctx
.dump_printf
= dump_ctx_printf
;
419 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
424 struct tgsi_iterate_context
*iter
,
425 struct tgsi_full_property
*prop
)
428 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
431 ENM(prop
->Property
.PropertyName
, tgsi_property_names
);
433 if (prop
->Property
.NrTokens
> 1)
436 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
437 switch (prop
->Property
.PropertyName
) {
438 case TGSI_PROPERTY_GS_INPUT_PRIM
:
439 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
440 ENM(prop
->u
[i
].Data
, tgsi_primitive_names
);
442 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
443 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_origin_names
);
445 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
446 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_pixel_center_names
);
449 SID( prop
->u
[i
].Data
);
452 if (i
< prop
->Property
.NrTokens
- 2)
460 void tgsi_dump_property(
461 const struct tgsi_full_property
*prop
)
465 ctx
.dump_printf
= dump_ctx_printf
;
467 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
472 struct tgsi_iterate_context
*iter
,
473 struct tgsi_full_immediate
*imm
)
475 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
480 ENM( imm
->Immediate
.DataType
, tgsi_immediate_type_names
);
482 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
483 imm
->Immediate
.DataType
);
492 const struct tgsi_full_immediate
*imm
)
496 ctx
.dump_printf
= dump_ctx_printf
;
498 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
503 struct tgsi_iterate_context
*iter
,
504 struct tgsi_full_instruction
*inst
)
506 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
507 uint instno
= ctx
->instno
++;
508 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
510 boolean first_reg
= TRUE
;
515 ctx
->indent
-= info
->pre_dedent
;
516 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
518 ctx
->indent
+= info
->post_indent
;
520 if (inst
->Instruction
.Predicate
) {
523 if (inst
->Predicate
.Negate
)
527 SID( inst
->Predicate
.Index
);
530 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
531 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
532 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
533 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
535 ENM( inst
->Predicate
.SwizzleX
, tgsi_swizzle_names
);
536 ENM( inst
->Predicate
.SwizzleY
, tgsi_swizzle_names
);
537 ENM( inst
->Predicate
.SwizzleZ
, tgsi_swizzle_names
);
538 ENM( inst
->Predicate
.SwizzleW
, tgsi_swizzle_names
);
544 TXT( info
->mnemonic
);
546 if (inst
->Instruction
.Saturate
) {
550 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
551 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
557 _dump_register_dst( ctx
, dst
);
558 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
563 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
564 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
570 if (src
->Register
.Negate
)
572 if (src
->Register
.Absolute
)
575 _dump_register_src(ctx
, src
);
577 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
578 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
579 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
580 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
582 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
583 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
584 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
585 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
588 if (src
->Register
.Absolute
)
594 if (inst
->Instruction
.Texture
) {
595 if (!(inst
->Instruction
.Opcode
>= TGSI_OPCODE_SAMPLE
&&
596 inst
->Instruction
.Opcode
<= TGSI_OPCODE_GATHER4
)) {
598 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
600 for (i
= 0; i
< inst
->Texture
.NumOffsets
; i
++) {
602 TXT(tgsi_file_name(inst
->TexOffsets
[i
].File
));
604 SID( inst
->TexOffsets
[i
].Index
);
607 ENM( inst
->TexOffsets
[i
].SwizzleX
, tgsi_swizzle_names
);
608 ENM( inst
->TexOffsets
[i
].SwizzleY
, tgsi_swizzle_names
);
609 ENM( inst
->TexOffsets
[i
].SwizzleZ
, tgsi_swizzle_names
);
613 switch (inst
->Instruction
.Opcode
) {
615 case TGSI_OPCODE_UIF
:
616 case TGSI_OPCODE_ELSE
:
617 case TGSI_OPCODE_BGNLOOP
:
618 case TGSI_OPCODE_ENDLOOP
:
619 case TGSI_OPCODE_CAL
:
620 case TGSI_OPCODE_BGNSUB
:
622 UID( inst
->Label
.Label
);
626 /* update indentation */
627 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
628 inst
->Instruction
.Opcode
== TGSI_OPCODE_UIF
||
629 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
630 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
631 ctx
->indentation
+= indent_spaces
;
640 tgsi_dump_instruction(
641 const struct tgsi_full_instruction
*inst
,
649 ctx
.dump_printf
= dump_ctx_printf
;
653 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
658 struct tgsi_iterate_context
*iter
)
660 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
661 ENM( iter
->processor
.Processor
, tgsi_processor_type_names
);
667 tgsi_dump_to_file(const struct tgsi_token
*tokens
, uint flags
, FILE *file
)
671 ctx
.iter
.prolog
= prolog
;
672 ctx
.iter
.iterate_instruction
= iter_instruction
;
673 ctx
.iter
.iterate_declaration
= iter_declaration
;
674 ctx
.iter
.iterate_immediate
= iter_immediate
;
675 ctx
.iter
.iterate_property
= iter_property
;
676 ctx
.iter
.epilog
= NULL
;
681 ctx
.dump_printf
= dump_ctx_printf
;
685 tgsi_iterate_shader( tokens
, &ctx
.iter
);
689 tgsi_dump(const struct tgsi_token
*tokens
, uint flags
)
691 tgsi_dump_to_file(tokens
, flags
, NULL
);
696 struct dump_ctx base
;
703 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
705 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
710 va_start(ap
, format
);
711 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
714 /* Some complicated logic needed to handle the return value of
718 written
= MIN2(sctx
->left
, written
);
719 sctx
->ptr
+= written
;
720 sctx
->left
-= written
;
727 const struct tgsi_token
*tokens
,
732 struct str_dump_ctx ctx
;
734 ctx
.base
.iter
.prolog
= prolog
;
735 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
736 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
737 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
738 ctx
.base
.iter
.iterate_property
= iter_property
;
739 ctx
.base
.iter
.epilog
= NULL
;
744 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
745 ctx
.base
.indentation
= 0;
746 ctx
.base
.file
= NULL
;
751 ctx
.left
= (int)size
;
753 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);
757 tgsi_dump_instruction_str(
758 const struct tgsi_full_instruction
*inst
,
763 struct str_dump_ctx ctx
;
765 ctx
.base
.instno
= instno
;
766 ctx
.base
.immno
= instno
;
768 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
769 ctx
.base
.indentation
= 0;
770 ctx
.base
.file
= NULL
;
775 ctx
.left
= (int)size
;
777 iter_instruction( &ctx
.base
.iter
, (struct tgsi_full_instruction
*)inst
);