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
[] =
135 static const char *swizzle_names
[] =
143 static const char *texture_names
[] =
156 static const char *property_names
[] =
158 "GS_INPUT_PRIMITIVE",
159 "GS_OUTPUT_PRIMITIVE",
160 "GS_MAX_OUTPUT_VERTICES"
163 static const char *primitive_names
[] =
180 struct dump_ctx
*ctx
,
185 ENM( file
, file_names
);
187 /* all geometry shader inputs are two dimensional */
188 if (file
== TGSI_FILE_INPUT
&&
189 ctx
->iter
.processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
)
203 struct dump_ctx
*ctx
,
207 ENM( file
, file_names
);
217 struct dump_ctx
*ctx
,
218 const struct tgsi_full_src_register
*src
)
220 if (src
->Register
.Indirect
) {
221 ENM( src
->Register
.File
, file_names
);
223 ENM( src
->Indirect
.File
, file_names
);
225 SID( src
->Indirect
.Index
);
227 ENM( src
->Indirect
.SwizzleX
, swizzle_names
);
228 if (src
->Register
.Index
!= 0) {
229 if (src
->Register
.Index
> 0)
231 SID( src
->Register
.Index
);
235 ENM( src
->Register
.File
, file_names
);
237 SID( src
->Register
.Index
);
240 if (src
->Register
.Dimension
) {
242 SID( src
->Dimension
.Index
);
249 struct dump_ctx
*ctx
,
256 ENM( file
, file_names
);
258 ENM( ind_file
, file_names
);
262 ENM( ind_swizzle
, swizzle_names
);
273 struct dump_ctx
*ctx
,
276 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
278 if (writemask
& TGSI_WRITEMASK_X
)
280 if (writemask
& TGSI_WRITEMASK_Y
)
282 if (writemask
& TGSI_WRITEMASK_Z
)
284 if (writemask
& TGSI_WRITEMASK_W
)
291 struct tgsi_iterate_context
*iter
,
292 struct tgsi_full_declaration
*decl
)
294 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
296 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
297 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
303 decl
->Declaration
.File
,
308 decl
->Declaration
.UsageMask
);
310 if (decl
->Declaration
.Semantic
) {
312 ENM( decl
->Semantic
.Name
, semantic_names
);
313 if (decl
->Semantic
.Index
!= 0 ||
314 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
316 UID( decl
->Semantic
.Index
);
321 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
322 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
325 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
328 if (decl
->Declaration
.Centroid
) {
332 if (decl
->Declaration
.Invariant
) {
333 TXT( ", INVARIANT" );
342 tgsi_dump_declaration(
343 const struct tgsi_full_declaration
*decl
)
347 ctx
.printf
= dump_ctx_printf
;
349 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
354 struct tgsi_iterate_context
*iter
,
355 struct tgsi_full_property
*prop
)
358 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
360 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
363 ENM(prop
->Property
.PropertyName
, property_names
);
365 if (prop
->Property
.NrTokens
> 1)
368 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
369 switch (prop
->Property
.PropertyName
) {
370 case TGSI_PROPERTY_GS_INPUT_PRIM
:
371 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
372 ENM(prop
->u
[i
].Data
, primitive_names
);
375 SID( prop
->u
[i
].Data
);
378 if (i
< prop
->Property
.NrTokens
- 2)
386 void tgsi_dump_property(
387 const struct tgsi_full_property
*prop
)
391 ctx
.printf
= dump_ctx_printf
;
393 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
398 struct tgsi_iterate_context
*iter
,
399 struct tgsi_full_immediate
*imm
)
401 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
406 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
410 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
411 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
412 switch (imm
->Immediate
.DataType
) {
413 case TGSI_IMM_FLOAT32
:
414 FLT( imm
->u
[i
].Float
);
420 if (i
< imm
->Immediate
.NrTokens
- 2)
432 const struct tgsi_full_immediate
*imm
)
436 ctx
.printf
= dump_ctx_printf
;
438 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
443 struct tgsi_iterate_context
*iter
,
444 struct tgsi_full_instruction
*inst
)
446 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
447 uint instno
= ctx
->instno
++;
448 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
450 boolean first_reg
= TRUE
;
455 ctx
->indent
-= info
->pre_dedent
;
456 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
458 ctx
->indent
+= info
->post_indent
;
460 TXT( info
->mnemonic
);
462 switch (inst
->Instruction
.Saturate
) {
465 case TGSI_SAT_ZERO_ONE
:
468 case TGSI_SAT_MINUS_PLUS_ONE
:
475 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
476 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
482 if (dst
->Register
.Indirect
) {
489 dst
->Indirect
.SwizzleX
);
495 dst
->Register
.Index
);
497 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
502 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
503 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
509 if (src
->Register
.Negate
)
511 if (src
->Register
.Absolute
)
514 _dump_register_src(ctx
, src
);
516 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
517 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
518 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
519 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
521 ENM( src
->Register
.SwizzleX
, swizzle_names
);
522 ENM( src
->Register
.SwizzleY
, swizzle_names
);
523 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
524 ENM( src
->Register
.SwizzleW
, swizzle_names
);
527 if (src
->Register
.Absolute
)
529 if (src
->Register
.Negate
)
535 if (inst
->Instruction
.Texture
) {
537 ENM( inst
->Texture
.Texture
, texture_names
);
540 switch (inst
->Instruction
.Opcode
) {
542 case TGSI_OPCODE_ELSE
:
543 case TGSI_OPCODE_BGNLOOP
:
544 case TGSI_OPCODE_ENDLOOP
:
545 case TGSI_OPCODE_CAL
:
547 UID( inst
->Label
.Label
);
551 /* update indentation */
552 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
553 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
554 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
555 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
556 ctx
->indentation
+= indent_spaces
;
565 tgsi_dump_instruction(
566 const struct tgsi_full_instruction
*inst
,
573 ctx
.printf
= dump_ctx_printf
;
576 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
581 struct tgsi_iterate_context
*iter
)
583 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
584 ENM( iter
->processor
.Processor
, processor_type_names
);
591 const struct tgsi_token
*tokens
,
596 ctx
.iter
.prolog
= prolog
;
597 ctx
.iter
.iterate_instruction
= iter_instruction
;
598 ctx
.iter
.iterate_declaration
= iter_declaration
;
599 ctx
.iter
.iterate_immediate
= iter_immediate
;
600 ctx
.iter
.iterate_property
= iter_property
;
601 ctx
.iter
.epilog
= NULL
;
605 ctx
.printf
= dump_ctx_printf
;
608 tgsi_iterate_shader( tokens
, &ctx
.iter
);
613 struct dump_ctx base
;
620 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
622 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
627 va_start(ap
, format
);
628 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
631 /* Some complicated logic needed to handle the return value of
635 written
= MIN2(sctx
->left
, written
);
636 sctx
->ptr
+= written
;
637 sctx
->left
-= written
;
644 const struct tgsi_token
*tokens
,
649 struct str_dump_ctx ctx
;
651 ctx
.base
.iter
.prolog
= prolog
;
652 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
653 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
654 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
655 ctx
.base
.iter
.iterate_property
= iter_property
;
656 ctx
.base
.iter
.epilog
= NULL
;
660 ctx
.base
.printf
= &str_dump_ctx_printf
;
661 ctx
.base
.indentation
= 0;
666 ctx
.left
= (int)size
;
668 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);