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
;
52 void (*dump_printf
)(struct dump_ctx
*ctx
, const char *format
, ...);
56 dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
61 _debug_vprintf(format
, ap
);
73 ctx
->dump_printf( ctx
, "%u", e
);
75 ctx
->dump_printf( ctx
, "%s", enums
[e
] );
78 #define EOL() ctx->dump_printf( ctx, "\n" )
79 #define TXT(S) ctx->dump_printf( ctx, "%s", S )
80 #define CHR(C) ctx->dump_printf( ctx, "%c", C )
81 #define UIX(I) ctx->dump_printf( ctx, "0x%x", I )
82 #define UID(I) ctx->dump_printf( ctx, "%u", I )
83 #define INSTID(I) ctx->dump_printf( ctx, "% 3u", I )
84 #define SID(I) ctx->dump_printf( ctx, "%d", I )
85 #define FLT(F) ctx->dump_printf( ctx, "%10.4f", F )
86 #define DBL(D) ctx->dump_printf( ctx, "%10.8f", D )
87 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
90 tgsi_swizzle_names
[4] =
100 struct dump_ctx
*ctx
,
101 const struct tgsi_full_src_register
*src
)
103 TXT(tgsi_file_name(src
->Register
.File
));
104 if (src
->Register
.Dimension
) {
105 if (src
->Dimension
.Indirect
) {
107 TXT(tgsi_file_name(src
->DimIndirect
.File
));
109 SID( src
->DimIndirect
.Index
);
111 ENM( src
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
112 if (src
->Dimension
.Index
!= 0) {
113 if (src
->Dimension
.Index
> 0)
115 SID( src
->Dimension
.Index
);
118 if (src
->DimIndirect
.ArrayID
) {
120 SID( src
->DimIndirect
.ArrayID
);
125 SID(src
->Dimension
.Index
);
129 if (src
->Register
.Indirect
) {
131 TXT(tgsi_file_name(src
->Indirect
.File
));
133 SID( src
->Indirect
.Index
);
135 ENM( src
->Indirect
.Swizzle
, tgsi_swizzle_names
);
136 if (src
->Register
.Index
!= 0) {
137 if (src
->Register
.Index
> 0)
139 SID( src
->Register
.Index
);
142 if (src
->Indirect
.ArrayID
) {
144 SID( src
->Indirect
.ArrayID
);
149 SID( src
->Register
.Index
);
157 struct dump_ctx
*ctx
,
158 const struct tgsi_full_dst_register
*dst
)
160 TXT(tgsi_file_name(dst
->Register
.File
));
161 if (dst
->Register
.Dimension
) {
162 if (dst
->Dimension
.Indirect
) {
164 TXT(tgsi_file_name(dst
->DimIndirect
.File
));
166 SID( dst
->DimIndirect
.Index
);
168 ENM( dst
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
169 if (dst
->Dimension
.Index
!= 0) {
170 if (dst
->Dimension
.Index
> 0)
172 SID( dst
->Dimension
.Index
);
175 if (dst
->DimIndirect
.ArrayID
) {
177 SID( dst
->DimIndirect
.ArrayID
);
182 SID(dst
->Dimension
.Index
);
186 if (dst
->Register
.Indirect
) {
188 TXT(tgsi_file_name(dst
->Indirect
.File
));
190 SID( dst
->Indirect
.Index
);
192 ENM( dst
->Indirect
.Swizzle
, tgsi_swizzle_names
);
193 if (dst
->Register
.Index
!= 0) {
194 if (dst
->Register
.Index
> 0)
196 SID( dst
->Register
.Index
);
199 if (dst
->Indirect
.ArrayID
) {
201 SID( dst
->Indirect
.ArrayID
);
206 SID( dst
->Register
.Index
);
212 struct dump_ctx
*ctx
,
215 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
217 if (writemask
& TGSI_WRITEMASK_X
)
219 if (writemask
& TGSI_WRITEMASK_Y
)
221 if (writemask
& TGSI_WRITEMASK_Z
)
223 if (writemask
& TGSI_WRITEMASK_W
)
229 dump_imm_data(struct tgsi_iterate_context
*iter
,
230 union tgsi_immediate_data
*data
,
234 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
239 assert( num_tokens
<= 4 );
240 for (i
= 0; i
< num_tokens
; i
++) {
242 case TGSI_IMM_FLOAT64
: {
244 d
.ui
= data
[i
].Uint
| (uint64_t)data
[i
+1].Uint
<< 32;
249 case TGSI_IMM_FLOAT32
:
250 FLT( data
[i
].Float
);
252 case TGSI_IMM_UINT32
:
262 if (i
< num_tokens
- 1)
270 struct tgsi_iterate_context
*iter
,
271 struct tgsi_full_declaration
*decl
)
273 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
277 TXT(tgsi_file_name(decl
->Declaration
.File
));
279 /* all geometry shader inputs are two dimensional */
280 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
281 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
285 if (decl
->Declaration
.Dimension
) {
287 SID(decl
->Dim
.Index2D
);
292 SID(decl
->Range
.First
);
293 if (decl
->Range
.First
!= decl
->Range
.Last
) {
295 SID(decl
->Range
.Last
);
301 decl
->Declaration
.UsageMask
);
303 if (decl
->Declaration
.Array
) {
305 SID(decl
->Array
.ArrayID
);
309 if (decl
->Declaration
.Local
)
312 if (decl
->Declaration
.Semantic
) {
314 ENM( decl
->Semantic
.Name
, tgsi_semantic_names
);
315 if (decl
->Semantic
.Index
!= 0 ||
316 decl
->Semantic
.Name
== TGSI_SEMANTIC_TEXCOORD
||
317 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
319 UID( decl
->Semantic
.Index
);
324 if (decl
->Declaration
.File
== TGSI_FILE_RESOURCE
) {
326 ENM(decl
->Resource
.Resource
, tgsi_texture_names
);
327 if (decl
->Resource
.Writable
)
329 if (decl
->Resource
.Raw
)
333 if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER_VIEW
) {
335 ENM(decl
->SamplerView
.Resource
, tgsi_texture_names
);
337 if ((decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeY
) &&
338 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeZ
) &&
339 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeW
)) {
340 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
342 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_return_type_names
);
344 ENM(decl
->SamplerView
.ReturnTypeY
, tgsi_return_type_names
);
346 ENM(decl
->SamplerView
.ReturnTypeZ
, tgsi_return_type_names
);
348 ENM(decl
->SamplerView
.ReturnTypeW
, tgsi_return_type_names
);
352 if (decl
->Declaration
.Interpolate
) {
353 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
354 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
357 ENM( decl
->Interp
.Interpolate
, tgsi_interpolate_names
);
360 if (decl
->Interp
.Location
!= TGSI_INTERPOLATE_LOC_CENTER
) {
362 ENM( decl
->Interp
.Location
, tgsi_interpolate_locations
);
365 if (decl
->Interp
.CylindricalWrap
) {
367 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
370 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
373 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
376 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
382 if (decl
->Declaration
.Invariant
) {
383 TXT( ", INVARIANT" );
392 tgsi_dump_declaration(
393 const struct tgsi_full_declaration
*decl
)
397 ctx
.dump_printf
= dump_ctx_printf
;
399 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
404 struct tgsi_iterate_context
*iter
,
405 struct tgsi_full_property
*prop
)
408 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
411 ENM(prop
->Property
.PropertyName
, tgsi_property_names
);
413 if (prop
->Property
.NrTokens
> 1)
416 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
417 switch (prop
->Property
.PropertyName
) {
418 case TGSI_PROPERTY_GS_INPUT_PRIM
:
419 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
420 ENM(prop
->u
[i
].Data
, tgsi_primitive_names
);
422 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
423 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_origin_names
);
425 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
426 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_pixel_center_names
);
429 SID( prop
->u
[i
].Data
);
432 if (i
< prop
->Property
.NrTokens
- 2)
440 void tgsi_dump_property(
441 const struct tgsi_full_property
*prop
)
445 ctx
.dump_printf
= dump_ctx_printf
;
447 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
452 struct tgsi_iterate_context
*iter
,
453 struct tgsi_full_immediate
*imm
)
455 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
460 ENM( imm
->Immediate
.DataType
, tgsi_immediate_type_names
);
462 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
463 imm
->Immediate
.DataType
);
472 const struct tgsi_full_immediate
*imm
)
476 ctx
.dump_printf
= dump_ctx_printf
;
478 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
483 struct tgsi_iterate_context
*iter
,
484 struct tgsi_full_instruction
*inst
)
486 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
487 uint instno
= ctx
->instno
++;
488 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
490 boolean first_reg
= TRUE
;
495 ctx
->indent
-= info
->pre_dedent
;
496 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
498 ctx
->indent
+= info
->post_indent
;
500 if (inst
->Instruction
.Predicate
) {
503 if (inst
->Predicate
.Negate
)
507 SID( inst
->Predicate
.Index
);
510 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
511 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
512 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
513 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
515 ENM( inst
->Predicate
.SwizzleX
, tgsi_swizzle_names
);
516 ENM( inst
->Predicate
.SwizzleY
, tgsi_swizzle_names
);
517 ENM( inst
->Predicate
.SwizzleZ
, tgsi_swizzle_names
);
518 ENM( inst
->Predicate
.SwizzleW
, tgsi_swizzle_names
);
524 TXT( info
->mnemonic
);
526 switch (inst
->Instruction
.Saturate
) {
529 case TGSI_SAT_ZERO_ONE
:
532 case TGSI_SAT_MINUS_PLUS_ONE
:
539 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
540 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
546 _dump_register_dst( ctx
, dst
);
547 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
552 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
553 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
559 if (src
->Register
.Negate
)
561 if (src
->Register
.Absolute
)
564 _dump_register_src(ctx
, src
);
566 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
567 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
568 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
569 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
571 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
572 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
573 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
574 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
577 if (src
->Register
.Absolute
)
583 if (inst
->Instruction
.Texture
) {
584 if (!(inst
->Instruction
.Opcode
>= TGSI_OPCODE_SAMPLE
&&
585 inst
->Instruction
.Opcode
<= TGSI_OPCODE_GATHER4
)) {
587 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
589 for (i
= 0; i
< inst
->Texture
.NumOffsets
; i
++) {
591 TXT(tgsi_file_name(inst
->TexOffsets
[i
].File
));
593 SID( inst
->TexOffsets
[i
].Index
);
596 ENM( inst
->TexOffsets
[i
].SwizzleX
, tgsi_swizzle_names
);
597 ENM( inst
->TexOffsets
[i
].SwizzleY
, tgsi_swizzle_names
);
598 ENM( inst
->TexOffsets
[i
].SwizzleZ
, tgsi_swizzle_names
);
602 switch (inst
->Instruction
.Opcode
) {
604 case TGSI_OPCODE_UIF
:
605 case TGSI_OPCODE_ELSE
:
606 case TGSI_OPCODE_BGNLOOP
:
607 case TGSI_OPCODE_ENDLOOP
:
608 case TGSI_OPCODE_CAL
:
609 case TGSI_OPCODE_BGNSUB
:
611 UID( inst
->Label
.Label
);
615 /* update indentation */
616 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
617 inst
->Instruction
.Opcode
== TGSI_OPCODE_UIF
||
618 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
619 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
620 ctx
->indentation
+= indent_spaces
;
629 tgsi_dump_instruction(
630 const struct tgsi_full_instruction
*inst
,
638 ctx
.dump_printf
= dump_ctx_printf
;
641 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
646 struct tgsi_iterate_context
*iter
)
648 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
649 ENM( iter
->processor
.Processor
, tgsi_processor_type_names
);
656 const struct tgsi_token
*tokens
,
661 ctx
.iter
.prolog
= prolog
;
662 ctx
.iter
.iterate_instruction
= iter_instruction
;
663 ctx
.iter
.iterate_declaration
= iter_declaration
;
664 ctx
.iter
.iterate_immediate
= iter_immediate
;
665 ctx
.iter
.iterate_property
= iter_property
;
666 ctx
.iter
.epilog
= NULL
;
671 ctx
.dump_printf
= dump_ctx_printf
;
674 tgsi_iterate_shader( tokens
, &ctx
.iter
);
679 struct dump_ctx base
;
686 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
688 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
693 va_start(ap
, format
);
694 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
697 /* Some complicated logic needed to handle the return value of
701 written
= MIN2(sctx
->left
, written
);
702 sctx
->ptr
+= written
;
703 sctx
->left
-= written
;
710 const struct tgsi_token
*tokens
,
715 struct str_dump_ctx ctx
;
717 ctx
.base
.iter
.prolog
= prolog
;
718 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
719 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
720 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
721 ctx
.base
.iter
.iterate_property
= iter_property
;
722 ctx
.base
.iter
.epilog
= NULL
;
727 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
728 ctx
.base
.indentation
= 0;
733 ctx
.left
= (int)size
;
735 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);
739 tgsi_dump_instruction_str(
740 const struct tgsi_full_instruction
*inst
,
745 struct str_dump_ctx ctx
;
747 ctx
.base
.instno
= instno
;
748 ctx
.base
.immno
= instno
;
750 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
751 ctx
.base
.indentation
= 0;
756 ctx
.left
= (int)size
;
758 iter_instruction( &ctx
.base
.iter
, (struct tgsi_full_instruction
*)inst
);