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
[] =
129 static const char *immediate_type_names
[] =
134 static const char *swizzle_names
[] =
142 static const char *texture_names
[] =
155 static const char *property_names
[] =
157 "GS_INPUT_PRIMITIVE",
158 "GS_OUTPUT_PRIMITIVE",
159 "GS_MAX_OUTPUT_VERTICES"
162 static const char *primitive_names
[] =
179 struct dump_ctx
*ctx
,
184 ENM( file
, file_names
);
186 /* all geometry shader inputs are two dimensional */
187 if (file
== TGSI_FILE_INPUT
&&
188 ctx
->iter
.processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
)
202 struct dump_ctx
*ctx
,
206 ENM( file
, file_names
);
216 struct dump_ctx
*ctx
,
217 const struct tgsi_full_src_register
*src
)
219 if (src
->Register
.Indirect
) {
220 ENM( src
->Register
.File
, file_names
);
222 ENM( src
->Indirect
.File
, file_names
);
224 SID( src
->Indirect
.Index
);
226 ENM( src
->Indirect
.SwizzleX
, swizzle_names
);
227 if (src
->Register
.Index
!= 0) {
228 if (src
->Register
.Index
> 0)
230 SID( src
->Register
.Index
);
234 ENM( src
->Register
.File
, file_names
);
236 SID( src
->Register
.Index
);
239 if (src
->Register
.Dimension
) {
241 SID( src
->Dimension
.Index
);
248 struct dump_ctx
*ctx
,
255 ENM( file
, file_names
);
257 ENM( ind_file
, file_names
);
261 ENM( ind_swizzle
, swizzle_names
);
272 struct dump_ctx
*ctx
,
275 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
277 if (writemask
& TGSI_WRITEMASK_X
)
279 if (writemask
& TGSI_WRITEMASK_Y
)
281 if (writemask
& TGSI_WRITEMASK_Z
)
283 if (writemask
& TGSI_WRITEMASK_W
)
290 struct tgsi_iterate_context
*iter
,
291 struct tgsi_full_declaration
*decl
)
293 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
295 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
296 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
302 decl
->Declaration
.File
,
307 decl
->Declaration
.UsageMask
);
309 if (decl
->Declaration
.Semantic
) {
311 ENM( decl
->Semantic
.Name
, semantic_names
);
312 if (decl
->Semantic
.Index
!= 0 ||
313 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
315 UID( decl
->Semantic
.Index
);
320 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
321 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
324 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
327 if (decl
->Declaration
.Centroid
) {
331 if (decl
->Declaration
.Invariant
) {
332 TXT( ", INVARIANT" );
341 tgsi_dump_declaration(
342 const struct tgsi_full_declaration
*decl
)
346 ctx
.printf
= dump_ctx_printf
;
348 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
353 struct tgsi_iterate_context
*iter
,
354 struct tgsi_full_property
*prop
)
357 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
359 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
362 ENM(prop
->Property
.PropertyName
, property_names
);
364 if (prop
->Property
.NrTokens
> 1)
367 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
368 switch (prop
->Property
.PropertyName
) {
369 case TGSI_PROPERTY_GS_INPUT_PRIM
:
370 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
371 ENM(prop
->u
[i
].Data
, primitive_names
);
374 SID( prop
->u
[i
].Data
);
377 if (i
< prop
->Property
.NrTokens
- 2)
385 void tgsi_dump_property(
386 const struct tgsi_full_property
*prop
)
390 ctx
.printf
= dump_ctx_printf
;
392 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
397 struct tgsi_iterate_context
*iter
,
398 struct tgsi_full_immediate
*imm
)
400 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
405 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
409 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
410 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
411 switch (imm
->Immediate
.DataType
) {
412 case TGSI_IMM_FLOAT32
:
413 FLT( imm
->u
[i
].Float
);
419 if (i
< imm
->Immediate
.NrTokens
- 2)
431 const struct tgsi_full_immediate
*imm
)
435 ctx
.printf
= dump_ctx_printf
;
437 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
442 struct tgsi_iterate_context
*iter
,
443 struct tgsi_full_instruction
*inst
)
445 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
446 uint instno
= ctx
->instno
++;
447 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
449 boolean first_reg
= TRUE
;
454 ctx
->indent
-= info
->pre_dedent
;
455 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
457 ctx
->indent
+= info
->post_indent
;
459 TXT( info
->mnemonic
);
461 switch (inst
->Instruction
.Saturate
) {
464 case TGSI_SAT_ZERO_ONE
:
467 case TGSI_SAT_MINUS_PLUS_ONE
:
474 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
475 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
481 if (dst
->Register
.Indirect
) {
488 dst
->Indirect
.SwizzleX
);
494 dst
->Register
.Index
);
496 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
501 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
502 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
508 if (src
->Register
.Negate
)
510 if (src
->Register
.Absolute
)
513 _dump_register_src(ctx
, src
);
515 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
516 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
517 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
518 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
520 ENM( src
->Register
.SwizzleX
, swizzle_names
);
521 ENM( src
->Register
.SwizzleY
, swizzle_names
);
522 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
523 ENM( src
->Register
.SwizzleW
, swizzle_names
);
526 if (src
->Register
.Absolute
)
532 if (inst
->Instruction
.Texture
) {
534 ENM( inst
->Texture
.Texture
, texture_names
);
537 switch (inst
->Instruction
.Opcode
) {
539 case TGSI_OPCODE_ELSE
:
540 case TGSI_OPCODE_BGNLOOP
:
541 case TGSI_OPCODE_ENDLOOP
:
542 case TGSI_OPCODE_CAL
:
544 UID( inst
->Label
.Label
);
548 /* update indentation */
549 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
550 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
551 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
552 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
553 ctx
->indentation
+= indent_spaces
;
562 tgsi_dump_instruction(
563 const struct tgsi_full_instruction
*inst
,
570 ctx
.printf
= dump_ctx_printf
;
573 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
578 struct tgsi_iterate_context
*iter
)
580 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
581 ENM( iter
->processor
.Processor
, processor_type_names
);
588 const struct tgsi_token
*tokens
,
593 ctx
.iter
.prolog
= prolog
;
594 ctx
.iter
.iterate_instruction
= iter_instruction
;
595 ctx
.iter
.iterate_declaration
= iter_declaration
;
596 ctx
.iter
.iterate_immediate
= iter_immediate
;
597 ctx
.iter
.iterate_property
= iter_property
;
598 ctx
.iter
.epilog
= NULL
;
602 ctx
.printf
= dump_ctx_printf
;
605 tgsi_iterate_shader( tokens
, &ctx
.iter
);
610 struct dump_ctx base
;
617 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
619 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
624 va_start(ap
, format
);
625 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
628 /* Some complicated logic needed to handle the return value of
632 written
= MIN2(sctx
->left
, written
);
633 sctx
->ptr
+= written
;
634 sctx
->left
-= written
;
641 const struct tgsi_token
*tokens
,
646 struct str_dump_ctx ctx
;
648 ctx
.base
.iter
.prolog
= prolog
;
649 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
650 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
651 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
652 ctx
.base
.iter
.iterate_property
= iter_property
;
653 ctx
.base
.iter
.epilog
= NULL
;
657 ctx
.base
.printf
= &str_dump_ctx_printf
;
658 ctx
.base
.indentation
= 0;
663 ctx
.left
= (int)size
;
665 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);