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
] =
108 static const char *interpolate_names
[] =
115 static const char *semantic_names
[] =
130 static const char *immediate_type_names
[] =
137 static const char *swizzle_names
[] =
145 static const char *texture_names
[] =
158 static const char *property_names
[] =
160 "GS_INPUT_PRIMITIVE",
161 "GS_OUTPUT_PRIMITIVE",
162 "GS_MAX_OUTPUT_VERTICES"
165 static const char *primitive_names
[] =
182 struct dump_ctx
*ctx
,
187 ENM( file
, file_names
);
189 /* all geometry shader inputs are two dimensional */
190 if (file
== TGSI_FILE_INPUT
&&
191 ctx
->iter
.processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
)
205 struct dump_ctx
*ctx
,
209 ENM( file
, file_names
);
219 struct dump_ctx
*ctx
,
220 const struct tgsi_full_src_register
*src
)
222 if (src
->Register
.Indirect
) {
223 ENM( src
->Register
.File
, file_names
);
225 ENM( src
->Indirect
.File
, file_names
);
227 SID( src
->Indirect
.Index
);
229 ENM( src
->Indirect
.SwizzleX
, swizzle_names
);
230 if (src
->Register
.Index
!= 0) {
231 if (src
->Register
.Index
> 0)
233 SID( src
->Register
.Index
);
237 ENM( src
->Register
.File
, file_names
);
239 SID( src
->Register
.Index
);
242 if (src
->Register
.Dimension
) {
244 SID( src
->Dimension
.Index
);
251 struct dump_ctx
*ctx
,
258 ENM( file
, file_names
);
260 ENM( ind_file
, file_names
);
264 ENM( ind_swizzle
, swizzle_names
);
275 struct dump_ctx
*ctx
,
278 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
280 if (writemask
& TGSI_WRITEMASK_X
)
282 if (writemask
& TGSI_WRITEMASK_Y
)
284 if (writemask
& TGSI_WRITEMASK_Z
)
286 if (writemask
& TGSI_WRITEMASK_W
)
293 struct tgsi_iterate_context
*iter
,
294 struct tgsi_full_declaration
*decl
)
296 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
298 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
299 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
305 decl
->Declaration
.File
,
310 decl
->Declaration
.UsageMask
);
312 if (decl
->Declaration
.Semantic
) {
314 ENM( decl
->Semantic
.Name
, semantic_names
);
315 if (decl
->Semantic
.Index
!= 0 ||
316 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
318 UID( decl
->Semantic
.Index
);
323 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
324 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
327 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
330 if (decl
->Declaration
.Centroid
) {
334 if (decl
->Declaration
.Invariant
) {
335 TXT( ", INVARIANT" );
344 tgsi_dump_declaration(
345 const struct tgsi_full_declaration
*decl
)
349 ctx
.printf
= dump_ctx_printf
;
351 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
356 struct tgsi_iterate_context
*iter
,
357 struct tgsi_full_property
*prop
)
360 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
362 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
365 ENM(prop
->Property
.PropertyName
, property_names
);
367 if (prop
->Property
.NrTokens
> 1)
370 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
371 switch (prop
->Property
.PropertyName
) {
372 case TGSI_PROPERTY_GS_INPUT_PRIM
:
373 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
374 ENM(prop
->u
[i
].Data
, primitive_names
);
377 SID( prop
->u
[i
].Data
);
380 if (i
< prop
->Property
.NrTokens
- 2)
388 void tgsi_dump_property(
389 const struct tgsi_full_property
*prop
)
393 ctx
.printf
= dump_ctx_printf
;
395 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
400 struct tgsi_iterate_context
*iter
,
401 struct tgsi_full_immediate
*imm
)
403 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
408 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
412 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
413 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
414 switch (imm
->Immediate
.DataType
) {
415 case TGSI_IMM_FLOAT32
:
416 FLT( imm
->u
[i
].Float
);
418 case TGSI_IMM_UINT32
:
428 if (i
< imm
->Immediate
.NrTokens
- 2)
440 const struct tgsi_full_immediate
*imm
)
444 ctx
.printf
= dump_ctx_printf
;
446 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
451 struct tgsi_iterate_context
*iter
,
452 struct tgsi_full_instruction
*inst
)
454 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
455 uint instno
= ctx
->instno
++;
456 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
458 boolean first_reg
= TRUE
;
463 ctx
->indent
-= info
->pre_dedent
;
464 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
466 ctx
->indent
+= info
->post_indent
;
468 TXT( info
->mnemonic
);
470 switch (inst
->Instruction
.Saturate
) {
473 case TGSI_SAT_ZERO_ONE
:
476 case TGSI_SAT_MINUS_PLUS_ONE
:
483 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
484 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
490 if (dst
->Register
.Indirect
) {
497 dst
->Indirect
.SwizzleX
);
503 dst
->Register
.Index
);
505 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
510 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
511 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
517 if (src
->Register
.Negate
)
519 if (src
->Register
.Absolute
)
522 _dump_register_src(ctx
, src
);
524 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
525 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
526 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
527 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
529 ENM( src
->Register
.SwizzleX
, swizzle_names
);
530 ENM( src
->Register
.SwizzleY
, swizzle_names
);
531 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
532 ENM( src
->Register
.SwizzleW
, swizzle_names
);
535 if (src
->Register
.Absolute
)
541 if (inst
->Instruction
.Texture
) {
543 ENM( inst
->Texture
.Texture
, texture_names
);
546 switch (inst
->Instruction
.Opcode
) {
548 case TGSI_OPCODE_ELSE
:
549 case TGSI_OPCODE_BGNLOOP
:
550 case TGSI_OPCODE_ENDLOOP
:
551 case TGSI_OPCODE_CAL
:
553 UID( inst
->Label
.Label
);
557 /* update indentation */
558 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
559 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
560 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
561 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
562 ctx
->indentation
+= indent_spaces
;
571 tgsi_dump_instruction(
572 const struct tgsi_full_instruction
*inst
,
579 ctx
.printf
= dump_ctx_printf
;
582 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
587 struct tgsi_iterate_context
*iter
)
589 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
590 ENM( iter
->processor
.Processor
, processor_type_names
);
597 const struct tgsi_token
*tokens
,
602 ctx
.iter
.prolog
= prolog
;
603 ctx
.iter
.iterate_instruction
= iter_instruction
;
604 ctx
.iter
.iterate_declaration
= iter_declaration
;
605 ctx
.iter
.iterate_immediate
= iter_immediate
;
606 ctx
.iter
.iterate_property
= iter_property
;
607 ctx
.iter
.epilog
= NULL
;
611 ctx
.printf
= dump_ctx_printf
;
614 tgsi_iterate_shader( tokens
, &ctx
.iter
);
619 struct dump_ctx base
;
626 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
628 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
633 va_start(ap
, format
);
634 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
637 /* Some complicated logic needed to handle the return value of
641 written
= MIN2(sctx
->left
, written
);
642 sctx
->ptr
+= written
;
643 sctx
->left
-= written
;
650 const struct tgsi_token
*tokens
,
655 struct str_dump_ctx ctx
;
657 ctx
.base
.iter
.prolog
= prolog
;
658 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
659 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
660 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
661 ctx
.base
.iter
.iterate_property
= iter_property
;
662 ctx
.base
.iter
.epilog
= NULL
;
666 ctx
.base
.printf
= &str_dump_ctx_printf
;
667 ctx
.base
.indentation
= 0;
672 ctx
.left
= (int)size
;
674 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);