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 ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
89 tgsi_swizzle_names
[4] =
100 const struct tgsi_full_src_register
*src
)
102 TXT(tgsi_file_name(src
->Register
.File
));
103 if (src
->Register
.Dimension
) {
104 if (src
->Dimension
.Indirect
) {
106 TXT(tgsi_file_name(src
->DimIndirect
.File
));
108 SID( src
->DimIndirect
.Index
);
110 ENM( src
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
111 if (src
->Dimension
.Index
!= 0) {
112 if (src
->Dimension
.Index
> 0)
114 SID( src
->Dimension
.Index
);
117 if (src
->DimIndirect
.ArrayID
) {
119 SID( src
->DimIndirect
.ArrayID
);
124 SID(src
->Dimension
.Index
);
128 if (src
->Register
.Indirect
) {
130 TXT(tgsi_file_name(src
->Indirect
.File
));
132 SID( src
->Indirect
.Index
);
134 ENM( src
->Indirect
.Swizzle
, tgsi_swizzle_names
);
135 if (src
->Register
.Index
!= 0) {
136 if (src
->Register
.Index
> 0)
138 SID( src
->Register
.Index
);
141 if (src
->Indirect
.ArrayID
) {
143 SID( src
->Indirect
.ArrayID
);
148 SID( src
->Register
.Index
);
156 struct dump_ctx
*ctx
,
157 const struct tgsi_full_dst_register
*dst
)
159 TXT(tgsi_file_name(dst
->Register
.File
));
160 if (dst
->Register
.Dimension
) {
161 if (dst
->Dimension
.Indirect
) {
163 TXT(tgsi_file_name(dst
->DimIndirect
.File
));
165 SID( dst
->DimIndirect
.Index
);
167 ENM( dst
->DimIndirect
.Swizzle
, tgsi_swizzle_names
);
168 if (dst
->Dimension
.Index
!= 0) {
169 if (dst
->Dimension
.Index
> 0)
171 SID( dst
->Dimension
.Index
);
174 if (dst
->DimIndirect
.ArrayID
) {
176 SID( dst
->DimIndirect
.ArrayID
);
181 SID(dst
->Dimension
.Index
);
185 if (dst
->Register
.Indirect
) {
187 TXT(tgsi_file_name(dst
->Indirect
.File
));
189 SID( dst
->Indirect
.Index
);
191 ENM( dst
->Indirect
.Swizzle
, tgsi_swizzle_names
);
192 if (dst
->Register
.Index
!= 0) {
193 if (dst
->Register
.Index
> 0)
195 SID( dst
->Register
.Index
);
198 if (dst
->Indirect
.ArrayID
) {
200 SID( dst
->Indirect
.ArrayID
);
205 SID( dst
->Register
.Index
);
211 struct dump_ctx
*ctx
,
214 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
216 if (writemask
& TGSI_WRITEMASK_X
)
218 if (writemask
& TGSI_WRITEMASK_Y
)
220 if (writemask
& TGSI_WRITEMASK_Z
)
222 if (writemask
& TGSI_WRITEMASK_W
)
228 dump_imm_data(struct tgsi_iterate_context
*iter
,
229 union tgsi_immediate_data
*data
,
233 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
238 assert( num_tokens
<= 4 );
239 for (i
= 0; i
< num_tokens
; i
++) {
241 case TGSI_IMM_FLOAT32
:
242 FLT( data
[i
].Float
);
244 case TGSI_IMM_UINT32
:
254 if (i
< num_tokens
- 1)
262 struct tgsi_iterate_context
*iter
,
263 struct tgsi_full_declaration
*decl
)
265 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
269 TXT(tgsi_file_name(decl
->Declaration
.File
));
271 /* all geometry shader inputs are two dimensional */
272 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
273 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
277 if (decl
->Declaration
.Dimension
) {
279 SID(decl
->Dim
.Index2D
);
284 SID(decl
->Range
.First
);
285 if (decl
->Range
.First
!= decl
->Range
.Last
) {
287 SID(decl
->Range
.Last
);
293 decl
->Declaration
.UsageMask
);
295 if (decl
->Declaration
.Array
) {
297 SID(decl
->Array
.ArrayID
);
301 if (decl
->Declaration
.Local
)
304 if (decl
->Declaration
.Semantic
) {
306 ENM( decl
->Semantic
.Name
, tgsi_semantic_names
);
307 if (decl
->Semantic
.Index
!= 0 ||
308 decl
->Semantic
.Name
== TGSI_SEMANTIC_TEXCOORD
||
309 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
311 UID( decl
->Semantic
.Index
);
316 if (decl
->Declaration
.File
== TGSI_FILE_RESOURCE
) {
318 ENM(decl
->Resource
.Resource
, tgsi_texture_names
);
319 if (decl
->Resource
.Writable
)
321 if (decl
->Resource
.Raw
)
325 if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER_VIEW
) {
327 ENM(decl
->SamplerView
.Resource
, tgsi_texture_names
);
329 if ((decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeY
) &&
330 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeZ
) &&
331 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeW
)) {
332 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_type_names
);
334 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_type_names
);
336 ENM(decl
->SamplerView
.ReturnTypeY
, tgsi_type_names
);
338 ENM(decl
->SamplerView
.ReturnTypeZ
, tgsi_type_names
);
340 ENM(decl
->SamplerView
.ReturnTypeW
, tgsi_type_names
);
344 if (decl
->Declaration
.Interpolate
) {
345 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
346 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
349 ENM( decl
->Interp
.Interpolate
, tgsi_interpolate_names
);
352 if (decl
->Interp
.Location
!= TGSI_INTERPOLATE_LOC_CENTER
) {
354 ENM( decl
->Interp
.Location
, tgsi_interpolate_locations
);
357 if (decl
->Interp
.CylindricalWrap
) {
359 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
362 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
365 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
368 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
374 if (decl
->Declaration
.Invariant
) {
375 TXT( ", INVARIANT" );
384 tgsi_dump_declaration(
385 const struct tgsi_full_declaration
*decl
)
389 ctx
.dump_printf
= dump_ctx_printf
;
391 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
396 struct tgsi_iterate_context
*iter
,
397 struct tgsi_full_property
*prop
)
400 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
403 ENM(prop
->Property
.PropertyName
, tgsi_property_names
);
405 if (prop
->Property
.NrTokens
> 1)
408 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
409 switch (prop
->Property
.PropertyName
) {
410 case TGSI_PROPERTY_GS_INPUT_PRIM
:
411 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
412 ENM(prop
->u
[i
].Data
, tgsi_primitive_names
);
414 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
415 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_origin_names
);
417 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
418 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_pixel_center_names
);
421 SID( prop
->u
[i
].Data
);
424 if (i
< prop
->Property
.NrTokens
- 2)
432 void tgsi_dump_property(
433 const struct tgsi_full_property
*prop
)
437 ctx
.dump_printf
= dump_ctx_printf
;
439 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
444 struct tgsi_iterate_context
*iter
,
445 struct tgsi_full_immediate
*imm
)
447 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
452 ENM( imm
->Immediate
.DataType
, tgsi_immediate_type_names
);
454 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
455 imm
->Immediate
.DataType
);
464 const struct tgsi_full_immediate
*imm
)
468 ctx
.dump_printf
= dump_ctx_printf
;
470 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
475 struct tgsi_iterate_context
*iter
,
476 struct tgsi_full_instruction
*inst
)
478 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
479 uint instno
= ctx
->instno
++;
480 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
482 boolean first_reg
= TRUE
;
487 ctx
->indent
-= info
->pre_dedent
;
488 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
490 ctx
->indent
+= info
->post_indent
;
492 if (inst
->Instruction
.Predicate
) {
495 if (inst
->Predicate
.Negate
)
499 SID( inst
->Predicate
.Index
);
502 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
503 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
504 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
505 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
507 ENM( inst
->Predicate
.SwizzleX
, tgsi_swizzle_names
);
508 ENM( inst
->Predicate
.SwizzleY
, tgsi_swizzle_names
);
509 ENM( inst
->Predicate
.SwizzleZ
, tgsi_swizzle_names
);
510 ENM( inst
->Predicate
.SwizzleW
, tgsi_swizzle_names
);
516 TXT( info
->mnemonic
);
518 switch (inst
->Instruction
.Saturate
) {
521 case TGSI_SAT_ZERO_ONE
:
524 case TGSI_SAT_MINUS_PLUS_ONE
:
531 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
532 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
538 _dump_register_dst( ctx
, dst
);
539 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
544 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
545 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
551 if (src
->Register
.Negate
)
553 if (src
->Register
.Absolute
)
556 _dump_register_src(ctx
, src
);
558 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
559 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
560 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
561 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
563 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
564 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
565 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
566 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
569 if (src
->Register
.Absolute
)
575 if (inst
->Instruction
.Texture
) {
576 if (!(inst
->Instruction
.Opcode
>= TGSI_OPCODE_SAMPLE
&&
577 inst
->Instruction
.Opcode
<= TGSI_OPCODE_GATHER4
)) {
579 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
581 for (i
= 0; i
< inst
->Texture
.NumOffsets
; i
++) {
583 TXT(tgsi_file_name(inst
->TexOffsets
[i
].File
));
585 SID( inst
->TexOffsets
[i
].Index
);
588 ENM( inst
->TexOffsets
[i
].SwizzleX
, tgsi_swizzle_names
);
589 ENM( inst
->TexOffsets
[i
].SwizzleY
, tgsi_swizzle_names
);
590 ENM( inst
->TexOffsets
[i
].SwizzleZ
, tgsi_swizzle_names
);
594 switch (inst
->Instruction
.Opcode
) {
596 case TGSI_OPCODE_UIF
:
597 case TGSI_OPCODE_ELSE
:
598 case TGSI_OPCODE_BGNLOOP
:
599 case TGSI_OPCODE_ENDLOOP
:
600 case TGSI_OPCODE_CAL
:
602 UID( inst
->Label
.Label
);
606 /* update indentation */
607 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
608 inst
->Instruction
.Opcode
== TGSI_OPCODE_UIF
||
609 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
610 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
611 ctx
->indentation
+= indent_spaces
;
620 tgsi_dump_instruction(
621 const struct tgsi_full_instruction
*inst
,
629 ctx
.dump_printf
= dump_ctx_printf
;
632 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
637 struct tgsi_iterate_context
*iter
)
639 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
640 ENM( iter
->processor
.Processor
, tgsi_processor_type_names
);
647 const struct tgsi_token
*tokens
,
652 ctx
.iter
.prolog
= prolog
;
653 ctx
.iter
.iterate_instruction
= iter_instruction
;
654 ctx
.iter
.iterate_declaration
= iter_declaration
;
655 ctx
.iter
.iterate_immediate
= iter_immediate
;
656 ctx
.iter
.iterate_property
= iter_property
;
657 ctx
.iter
.epilog
= NULL
;
662 ctx
.dump_printf
= dump_ctx_printf
;
665 tgsi_iterate_shader( tokens
, &ctx
.iter
);
670 struct dump_ctx base
;
677 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
679 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
684 va_start(ap
, format
);
685 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
688 /* Some complicated logic needed to handle the return value of
692 written
= MIN2(sctx
->left
, written
);
693 sctx
->ptr
+= written
;
694 sctx
->left
-= written
;
701 const struct tgsi_token
*tokens
,
706 struct str_dump_ctx ctx
;
708 ctx
.base
.iter
.prolog
= prolog
;
709 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
710 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
711 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
712 ctx
.base
.iter
.iterate_property
= iter_property
;
713 ctx
.base
.iter
.epilog
= NULL
;
718 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
719 ctx
.base
.indentation
= 0;
724 ctx
.left
= (int)size
;
726 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);
730 tgsi_dump_instruction_str(
731 const struct tgsi_full_instruction
*inst
,
736 struct str_dump_ctx ctx
;
738 ctx
.base
.instno
= instno
;
739 ctx
.base
.immno
= instno
;
741 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
742 ctx
.base
.indentation
= 0;
747 ctx
.left
= (int)size
;
749 iter_instruction( &ctx
.base
.iter
, (struct tgsi_full_instruction
*)inst
);