1 /**************************************************************************
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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"
37 /** Number of spaces to indent for IF/LOOP/etc */
38 static const int indent_spaces
= 3;
43 struct tgsi_iterate_context iter
;
50 void (*printf
)(struct dump_ctx
*ctx
, const char *format
, ...);
54 dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
59 _debug_vprintf(format
, ap
);
71 ctx
->printf( ctx
, "%u", e
);
73 ctx
->printf( ctx
, "%s", enums
[e
] );
76 #define EOL() ctx->printf( ctx, "\n" )
77 #define TXT(S) ctx->printf( ctx, "%s", S )
78 #define CHR(C) ctx->printf( ctx, "%c", C )
79 #define UIX(I) ctx->printf( ctx, "0x%x", I )
80 #define UID(I) ctx->printf( ctx, "%u", I )
81 #define INSTID(I) ctx->printf( ctx, "% 3u", I )
82 #define SID(I) ctx->printf( ctx, "%d", I )
83 #define FLT(F) ctx->printf( ctx, "%10.4f", F )
84 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
86 static const char *processor_type_names
[] =
94 tgsi_file_names
[TGSI_FILE_COUNT
] =
110 static const char *interpolate_names
[] =
117 static const char *semantic_names
[] =
133 static const char *immediate_type_names
[] =
141 tgsi_swizzle_names
[] =
150 tgsi_texture_names
[] =
163 static const char *property_names
[] =
165 "GS_INPUT_PRIMITIVE",
166 "GS_OUTPUT_PRIMITIVE",
167 "GS_MAX_OUTPUT_VERTICES",
169 "FS_COORD_PIXEL_CENTER"
172 static const char *primitive_names
[] =
185 "LINE_STRIP_ADJACENCY",
186 "TRIANGLES_ADJACENCY",
187 "TRIANGLE_STRIP_ADJACENCY"
190 static const char *fs_coord_origin_names
[] =
196 static const char *fs_coord_pixel_center_names
[] =
205 struct dump_ctx
*ctx
,
206 const struct tgsi_full_src_register
*src
)
208 ENM(src
->Register
.File
, tgsi_file_names
);
209 if (src
->Register
.Dimension
) {
210 if (src
->Dimension
.Indirect
) {
212 ENM( src
->DimIndirect
.File
, tgsi_file_names
);
214 SID( src
->DimIndirect
.Index
);
216 ENM( src
->DimIndirect
.SwizzleX
, tgsi_swizzle_names
);
217 if (src
->Dimension
.Index
!= 0) {
218 if (src
->Dimension
.Index
> 0)
220 SID( src
->Dimension
.Index
);
225 SID(src
->Dimension
.Index
);
229 if (src
->Register
.Indirect
) {
231 ENM( src
->Indirect
.File
, tgsi_file_names
);
233 SID( src
->Indirect
.Index
);
235 ENM( src
->Indirect
.SwizzleX
, tgsi_swizzle_names
);
236 if (src
->Register
.Index
!= 0) {
237 if (src
->Register
.Index
> 0)
239 SID( src
->Register
.Index
);
244 SID( src
->Register
.Index
);
252 struct dump_ctx
*ctx
,
253 const struct tgsi_full_dst_register
*dst
)
255 ENM(dst
->Register
.File
, tgsi_file_names
);
256 if (dst
->Register
.Dimension
) {
257 if (dst
->Dimension
.Indirect
) {
259 ENM( dst
->DimIndirect
.File
, tgsi_file_names
);
261 SID( dst
->DimIndirect
.Index
);
263 ENM( dst
->DimIndirect
.SwizzleX
, tgsi_swizzle_names
);
264 if (dst
->Dimension
.Index
!= 0) {
265 if (dst
->Dimension
.Index
> 0)
267 SID( dst
->Dimension
.Index
);
272 SID(dst
->Dimension
.Index
);
276 if (dst
->Register
.Indirect
) {
278 ENM( dst
->Indirect
.File
, tgsi_file_names
);
280 SID( dst
->Indirect
.Index
);
282 ENM( dst
->Indirect
.SwizzleX
, tgsi_swizzle_names
);
283 if (dst
->Register
.Index
!= 0) {
284 if (dst
->Register
.Index
> 0)
286 SID( dst
->Register
.Index
);
291 SID( dst
->Register
.Index
);
297 struct dump_ctx
*ctx
,
300 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
302 if (writemask
& TGSI_WRITEMASK_X
)
304 if (writemask
& TGSI_WRITEMASK_Y
)
306 if (writemask
& TGSI_WRITEMASK_Z
)
308 if (writemask
& TGSI_WRITEMASK_W
)
314 dump_imm_data(struct tgsi_iterate_context
*iter
,
315 union tgsi_immediate_data
*data
,
319 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
324 assert( num_tokens
<= 4 );
325 for (i
= 0; i
< num_tokens
; i
++) {
327 case TGSI_IMM_FLOAT32
:
328 FLT( data
[i
].Float
);
330 case TGSI_IMM_UINT32
:
340 if (i
< num_tokens
- 1)
348 struct tgsi_iterate_context
*iter
,
349 struct tgsi_full_declaration
*decl
)
351 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
353 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
354 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
358 ENM(decl
->Declaration
.File
, tgsi_file_names
);
360 /* all geometry shader inputs are two dimensional */
361 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
362 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
366 if (decl
->Declaration
.Dimension
) {
368 SID(decl
->Dim
.Index2D
);
373 SID(decl
->Range
.First
);
374 if (decl
->Range
.First
!= decl
->Range
.Last
) {
376 SID(decl
->Range
.Last
);
382 decl
->Declaration
.UsageMask
);
384 if (decl
->Declaration
.Semantic
) {
386 ENM( decl
->Semantic
.Name
, semantic_names
);
387 if (decl
->Semantic
.Index
!= 0 ||
388 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
390 UID( decl
->Semantic
.Index
);
395 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
396 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
399 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
402 if (decl
->Declaration
.Centroid
) {
406 if (decl
->Declaration
.Invariant
) {
407 TXT( ", INVARIANT" );
410 if (decl
->Declaration
.CylindricalWrap
) {
412 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
415 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
418 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
421 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
426 if (decl
->Declaration
.File
== TGSI_FILE_IMMEDIATE_ARRAY
) {
428 char range_indent
[4];
432 if (decl
->Range
.Last
< 10)
433 range_indent
[0] = '\0';
434 else if (decl
->Range
.Last
< 100) {
435 range_indent
[0] = ' ';
436 range_indent
[1] = '\0';
437 } else if (decl
->Range
.Last
< 1000) {
438 range_indent
[0] = ' ';
439 range_indent
[1] = ' ';
440 range_indent
[2] = '\0';
442 range_indent
[0] = ' ';
443 range_indent
[1] = ' ';
444 range_indent
[2] = ' ';
445 range_indent
[3] = '\0';
448 dump_imm_data(iter
, decl
->ImmediateData
.u
,
449 4, TGSI_IMM_FLOAT32
);
450 for(i
= 1; i
<= decl
->Range
.Last
; ++i
) {
451 /* indent by strlen of:
452 * "DCL IMMX[0..1] {" */
456 dump_imm_data(iter
, decl
->ImmediateData
.u
+ i
,
457 4, TGSI_IMM_FLOAT32
);
469 tgsi_dump_declaration(
470 const struct tgsi_full_declaration
*decl
)
474 ctx
.printf
= dump_ctx_printf
;
476 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
481 struct tgsi_iterate_context
*iter
,
482 struct tgsi_full_property
*prop
)
485 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
487 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
490 ENM(prop
->Property
.PropertyName
, property_names
);
492 if (prop
->Property
.NrTokens
> 1)
495 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
496 switch (prop
->Property
.PropertyName
) {
497 case TGSI_PROPERTY_GS_INPUT_PRIM
:
498 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
499 ENM(prop
->u
[i
].Data
, primitive_names
);
501 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
502 ENM(prop
->u
[i
].Data
, fs_coord_origin_names
);
504 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
505 ENM(prop
->u
[i
].Data
, fs_coord_pixel_center_names
);
508 SID( prop
->u
[i
].Data
);
511 if (i
< prop
->Property
.NrTokens
- 2)
519 void tgsi_dump_property(
520 const struct tgsi_full_property
*prop
)
524 ctx
.printf
= dump_ctx_printf
;
526 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
531 struct tgsi_iterate_context
*iter
,
532 struct tgsi_full_immediate
*imm
)
534 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
537 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
539 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
540 imm
->Immediate
.DataType
);
549 const struct tgsi_full_immediate
*imm
)
553 ctx
.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 switch (inst
->Instruction
.Saturate
) {
606 case TGSI_SAT_ZERO_ONE
:
609 case TGSI_SAT_MINUS_PLUS_ONE
:
616 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
617 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
623 _dump_register_dst( ctx
, dst
);
624 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
629 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
630 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
636 if (src
->Register
.Negate
)
638 if (src
->Register
.Absolute
)
641 _dump_register_src(ctx
, src
);
643 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
644 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
645 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
646 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
648 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
649 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
650 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
651 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
654 if (src
->Register
.Absolute
)
660 if (inst
->Instruction
.Texture
) {
662 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
665 switch (inst
->Instruction
.Opcode
) {
667 case TGSI_OPCODE_ELSE
:
668 case TGSI_OPCODE_BGNLOOP
:
669 case TGSI_OPCODE_ENDLOOP
:
670 case TGSI_OPCODE_CAL
:
672 UID( inst
->Label
.Label
);
676 /* update indentation */
677 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
678 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
679 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
680 ctx
->indentation
+= indent_spaces
;
689 tgsi_dump_instruction(
690 const struct tgsi_full_instruction
*inst
,
697 ctx
.printf
= dump_ctx_printf
;
700 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
705 struct tgsi_iterate_context
*iter
)
707 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
708 ENM( iter
->processor
.Processor
, processor_type_names
);
715 const struct tgsi_token
*tokens
,
720 ctx
.iter
.prolog
= prolog
;
721 ctx
.iter
.iterate_instruction
= iter_instruction
;
722 ctx
.iter
.iterate_declaration
= iter_declaration
;
723 ctx
.iter
.iterate_immediate
= iter_immediate
;
724 ctx
.iter
.iterate_property
= iter_property
;
725 ctx
.iter
.epilog
= NULL
;
729 ctx
.printf
= dump_ctx_printf
;
732 tgsi_iterate_shader( tokens
, &ctx
.iter
);
737 struct dump_ctx base
;
744 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
746 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
751 va_start(ap
, format
);
752 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
755 /* Some complicated logic needed to handle the return value of
759 written
= MIN2(sctx
->left
, written
);
760 sctx
->ptr
+= written
;
761 sctx
->left
-= written
;
768 const struct tgsi_token
*tokens
,
773 struct str_dump_ctx ctx
;
775 ctx
.base
.iter
.prolog
= prolog
;
776 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
777 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
778 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
779 ctx
.base
.iter
.iterate_property
= iter_property
;
780 ctx
.base
.iter
.epilog
= NULL
;
784 ctx
.base
.printf
= &str_dump_ctx_printf
;
785 ctx
.base
.indentation
= 0;
790 ctx
.left
= (int)size
;
792 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);