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
[] =
93 static const char *file_names
[TGSI_FILE_COUNT
] =
107 static const char *interpolate_names
[] =
114 static const char *semantic_names
[] =
129 static const char *immediate_type_names
[] =
136 static const char *swizzle_names
[] =
144 static const char *texture_names
[] =
157 static const char *property_names
[] =
159 "GS_INPUT_PRIMITIVE",
160 "GS_OUTPUT_PRIMITIVE",
161 "GS_MAX_OUTPUT_VERTICES",
163 "FS_COORD_PIXEL_CENTER"
166 static const char *primitive_names
[] =
180 static const char *fs_coord_origin_names
[] =
186 static const char *fs_coord_pixel_center_names
[] =
195 struct dump_ctx
*ctx
,
199 ENM( file
, file_names
);
209 struct dump_ctx
*ctx
,
210 const struct tgsi_full_src_register
*src
)
212 ENM(src
->Register
.File
, file_names
);
213 if (src
->Register
.Dimension
) {
215 SID(src
->Dimension
.Index
);
218 if (src
->Register
.Indirect
) {
220 ENM( src
->Indirect
.File
, file_names
);
222 SID( src
->Indirect
.Index
);
224 ENM( src
->Indirect
.SwizzleX
, swizzle_names
);
225 if (src
->Register
.Index
!= 0) {
226 if (src
->Register
.Index
> 0)
228 SID( src
->Register
.Index
);
233 SID( src
->Register
.Index
);
240 struct dump_ctx
*ctx
,
247 ENM( file
, file_names
);
249 ENM( ind_file
, file_names
);
253 ENM( ind_swizzle
, swizzle_names
);
264 struct dump_ctx
*ctx
,
267 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
269 if (writemask
& TGSI_WRITEMASK_X
)
271 if (writemask
& TGSI_WRITEMASK_Y
)
273 if (writemask
& TGSI_WRITEMASK_Z
)
275 if (writemask
& TGSI_WRITEMASK_W
)
282 struct tgsi_iterate_context
*iter
,
283 struct tgsi_full_declaration
*decl
)
285 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
287 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
288 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
292 ENM(decl
->Declaration
.File
, file_names
);
294 /* all geometry shader inputs are two dimensional */
295 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
296 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
300 if (decl
->Declaration
.Dimension
) {
302 SID(decl
->Dim
.Index2D
);
307 SID(decl
->Range
.First
);
308 if (decl
->Range
.First
!= decl
->Range
.Last
) {
310 SID(decl
->Range
.Last
);
316 decl
->Declaration
.UsageMask
);
318 if (decl
->Declaration
.Semantic
) {
320 ENM( decl
->Semantic
.Name
, semantic_names
);
321 if (decl
->Semantic
.Index
!= 0 ||
322 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
324 UID( decl
->Semantic
.Index
);
329 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
330 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
333 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
336 if (decl
->Declaration
.Centroid
) {
340 if (decl
->Declaration
.Invariant
) {
341 TXT( ", INVARIANT" );
344 if (decl
->Declaration
.CylindricalWrap
) {
346 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
349 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
352 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
355 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
366 tgsi_dump_declaration(
367 const struct tgsi_full_declaration
*decl
)
371 ctx
.printf
= dump_ctx_printf
;
373 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
378 struct tgsi_iterate_context
*iter
,
379 struct tgsi_full_property
*prop
)
382 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
384 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
387 ENM(prop
->Property
.PropertyName
, property_names
);
389 if (prop
->Property
.NrTokens
> 1)
392 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
393 switch (prop
->Property
.PropertyName
) {
394 case TGSI_PROPERTY_GS_INPUT_PRIM
:
395 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
396 ENM(prop
->u
[i
].Data
, primitive_names
);
398 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
399 ENM(prop
->u
[i
].Data
, fs_coord_origin_names
);
401 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
402 ENM(prop
->u
[i
].Data
, fs_coord_pixel_center_names
);
405 SID( prop
->u
[i
].Data
);
408 if (i
< prop
->Property
.NrTokens
- 2)
416 void tgsi_dump_property(
417 const struct tgsi_full_property
*prop
)
421 ctx
.printf
= dump_ctx_printf
;
423 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
428 struct tgsi_iterate_context
*iter
,
429 struct tgsi_full_immediate
*imm
)
431 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
436 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
440 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
441 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
442 switch (imm
->Immediate
.DataType
) {
443 case TGSI_IMM_FLOAT32
:
444 FLT( imm
->u
[i
].Float
);
446 case TGSI_IMM_UINT32
:
456 if (i
< imm
->Immediate
.NrTokens
- 2)
468 const struct tgsi_full_immediate
*imm
)
472 ctx
.printf
= dump_ctx_printf
;
474 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
479 struct tgsi_iterate_context
*iter
,
480 struct tgsi_full_instruction
*inst
)
482 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
483 uint instno
= ctx
->instno
++;
484 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
486 boolean first_reg
= TRUE
;
491 ctx
->indent
-= info
->pre_dedent
;
492 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
494 ctx
->indent
+= info
->post_indent
;
496 if (inst
->Instruction
.Predicate
) {
499 if (inst
->Predicate
.Negate
)
503 SID( inst
->Predicate
.Index
);
506 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
507 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
508 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
509 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
511 ENM( inst
->Predicate
.SwizzleX
, swizzle_names
);
512 ENM( inst
->Predicate
.SwizzleY
, swizzle_names
);
513 ENM( inst
->Predicate
.SwizzleZ
, swizzle_names
);
514 ENM( inst
->Predicate
.SwizzleW
, swizzle_names
);
520 TXT( info
->mnemonic
);
522 switch (inst
->Instruction
.Saturate
) {
525 case TGSI_SAT_ZERO_ONE
:
528 case TGSI_SAT_MINUS_PLUS_ONE
:
535 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
536 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
542 if (dst
->Register
.Indirect
) {
549 dst
->Indirect
.SwizzleX
);
555 dst
->Register
.Index
);
557 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
562 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
563 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
569 if (src
->Register
.Negate
)
571 if (src
->Register
.Absolute
)
574 _dump_register_src(ctx
, src
);
576 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
577 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
578 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
579 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
581 ENM( src
->Register
.SwizzleX
, swizzle_names
);
582 ENM( src
->Register
.SwizzleY
, swizzle_names
);
583 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
584 ENM( src
->Register
.SwizzleW
, swizzle_names
);
587 if (src
->Register
.Absolute
)
593 if (inst
->Instruction
.Texture
) {
595 ENM( inst
->Texture
.Texture
, texture_names
);
598 switch (inst
->Instruction
.Opcode
) {
600 case TGSI_OPCODE_ELSE
:
601 case TGSI_OPCODE_BGNLOOP
:
602 case TGSI_OPCODE_ENDLOOP
:
603 case TGSI_OPCODE_CAL
:
605 UID( inst
->Label
.Label
);
609 /* update indentation */
610 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
611 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
612 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
613 ctx
->indentation
+= indent_spaces
;
622 tgsi_dump_instruction(
623 const struct tgsi_full_instruction
*inst
,
630 ctx
.printf
= dump_ctx_printf
;
633 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
638 struct tgsi_iterate_context
*iter
)
640 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
641 ENM( iter
->processor
.Processor
, processor_type_names
);
648 const struct tgsi_token
*tokens
,
653 ctx
.iter
.prolog
= prolog
;
654 ctx
.iter
.iterate_instruction
= iter_instruction
;
655 ctx
.iter
.iterate_declaration
= iter_declaration
;
656 ctx
.iter
.iterate_immediate
= iter_immediate
;
657 ctx
.iter
.iterate_property
= iter_property
;
658 ctx
.iter
.epilog
= NULL
;
662 ctx
.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
;
717 ctx
.base
.printf
= &str_dump_ctx_printf
;
718 ctx
.base
.indentation
= 0;
723 ctx
.left
= (int)size
;
725 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);