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
,
186 ENM( file
, file_names
);
196 struct dump_ctx
*ctx
,
197 const struct tgsi_full_src_register
*src
)
199 ENM(src
->Register
.File
, file_names
);
200 if (src
->Register
.Dimension
) {
202 SID(src
->Dimension
.Index
);
205 if (src
->Register
.Indirect
) {
207 ENM( src
->Indirect
.File
, file_names
);
209 SID( src
->Indirect
.Index
);
211 ENM( src
->Indirect
.SwizzleX
, swizzle_names
);
212 if (src
->Register
.Index
!= 0) {
213 if (src
->Register
.Index
> 0)
215 SID( src
->Register
.Index
);
220 SID( src
->Register
.Index
);
227 struct dump_ctx
*ctx
,
234 ENM( file
, file_names
);
236 ENM( ind_file
, file_names
);
240 ENM( ind_swizzle
, swizzle_names
);
251 struct dump_ctx
*ctx
,
254 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
256 if (writemask
& TGSI_WRITEMASK_X
)
258 if (writemask
& TGSI_WRITEMASK_Y
)
260 if (writemask
& TGSI_WRITEMASK_Z
)
262 if (writemask
& TGSI_WRITEMASK_W
)
269 struct tgsi_iterate_context
*iter
,
270 struct tgsi_full_declaration
*decl
)
272 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
274 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
275 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
279 ENM(decl
->Declaration
.File
, file_names
);
281 /* all geometry shader inputs are two dimensional */
282 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
283 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
287 if (decl
->Declaration
.Dimension
) {
289 SID(decl
->Dim
.Index2D
);
294 SID(decl
->Range
.First
);
295 if (decl
->Range
.First
!= decl
->Range
.Last
) {
297 SID(decl
->Range
.Last
);
303 decl
->Declaration
.UsageMask
);
305 if (decl
->Declaration
.Semantic
) {
307 ENM( decl
->Semantic
.Name
, semantic_names
);
308 if (decl
->Semantic
.Index
!= 0 ||
309 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
311 UID( decl
->Semantic
.Index
);
316 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
317 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
320 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
323 if (decl
->Declaration
.Centroid
) {
327 if (decl
->Declaration
.Invariant
) {
328 TXT( ", INVARIANT" );
337 tgsi_dump_declaration(
338 const struct tgsi_full_declaration
*decl
)
342 ctx
.printf
= dump_ctx_printf
;
344 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
349 struct tgsi_iterate_context
*iter
,
350 struct tgsi_full_property
*prop
)
353 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
355 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
358 ENM(prop
->Property
.PropertyName
, property_names
);
360 if (prop
->Property
.NrTokens
> 1)
363 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
364 switch (prop
->Property
.PropertyName
) {
365 case TGSI_PROPERTY_GS_INPUT_PRIM
:
366 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
367 ENM(prop
->u
[i
].Data
, primitive_names
);
370 SID( prop
->u
[i
].Data
);
373 if (i
< prop
->Property
.NrTokens
- 2)
381 void tgsi_dump_property(
382 const struct tgsi_full_property
*prop
)
386 ctx
.printf
= dump_ctx_printf
;
388 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
393 struct tgsi_iterate_context
*iter
,
394 struct tgsi_full_immediate
*imm
)
396 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
401 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
405 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
406 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
407 switch (imm
->Immediate
.DataType
) {
408 case TGSI_IMM_FLOAT32
:
409 FLT( imm
->u
[i
].Float
);
411 case TGSI_IMM_UINT32
:
421 if (i
< imm
->Immediate
.NrTokens
- 2)
433 const struct tgsi_full_immediate
*imm
)
437 ctx
.printf
= dump_ctx_printf
;
439 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
444 struct tgsi_iterate_context
*iter
,
445 struct tgsi_full_instruction
*inst
)
447 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
448 uint instno
= ctx
->instno
++;
449 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
451 boolean first_reg
= TRUE
;
456 ctx
->indent
-= info
->pre_dedent
;
457 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
459 ctx
->indent
+= info
->post_indent
;
461 TXT( info
->mnemonic
);
463 switch (inst
->Instruction
.Saturate
) {
466 case TGSI_SAT_ZERO_ONE
:
469 case TGSI_SAT_MINUS_PLUS_ONE
:
476 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
477 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
483 if (dst
->Register
.Indirect
) {
490 dst
->Indirect
.SwizzleX
);
496 dst
->Register
.Index
);
498 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
503 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
504 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
510 if (src
->Register
.Negate
)
512 if (src
->Register
.Absolute
)
515 _dump_register_src(ctx
, src
);
517 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
518 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
519 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
520 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
522 ENM( src
->Register
.SwizzleX
, swizzle_names
);
523 ENM( src
->Register
.SwizzleY
, swizzle_names
);
524 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
525 ENM( src
->Register
.SwizzleW
, swizzle_names
);
528 if (src
->Register
.Absolute
)
534 if (inst
->Instruction
.Texture
) {
536 ENM( inst
->Texture
.Texture
, texture_names
);
539 switch (inst
->Instruction
.Opcode
) {
541 case TGSI_OPCODE_ELSE
:
542 case TGSI_OPCODE_BGNLOOP
:
543 case TGSI_OPCODE_ENDLOOP
:
544 case TGSI_OPCODE_CAL
:
546 UID( inst
->Label
.Label
);
550 /* update indentation */
551 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
552 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
553 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
554 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
555 ctx
->indentation
+= indent_spaces
;
564 tgsi_dump_instruction(
565 const struct tgsi_full_instruction
*inst
,
572 ctx
.printf
= dump_ctx_printf
;
575 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
580 struct tgsi_iterate_context
*iter
)
582 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
583 ENM( iter
->processor
.Processor
, processor_type_names
);
590 const struct tgsi_token
*tokens
,
595 ctx
.iter
.prolog
= prolog
;
596 ctx
.iter
.iterate_instruction
= iter_instruction
;
597 ctx
.iter
.iterate_declaration
= iter_declaration
;
598 ctx
.iter
.iterate_immediate
= iter_immediate
;
599 ctx
.iter
.iterate_property
= iter_property
;
600 ctx
.iter
.epilog
= NULL
;
604 ctx
.printf
= dump_ctx_printf
;
607 tgsi_iterate_shader( tokens
, &ctx
.iter
);
612 struct dump_ctx base
;
619 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
621 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
626 va_start(ap
, format
);
627 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
630 /* Some complicated logic needed to handle the return value of
634 written
= MIN2(sctx
->left
, written
);
635 sctx
->ptr
+= written
;
636 sctx
->left
-= written
;
643 const struct tgsi_token
*tokens
,
648 struct str_dump_ctx ctx
;
650 ctx
.base
.iter
.prolog
= prolog
;
651 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
652 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
653 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
654 ctx
.base
.iter
.iterate_property
= iter_property
;
655 ctx
.base
.iter
.epilog
= NULL
;
659 ctx
.base
.printf
= &str_dump_ctx_printf
;
660 ctx
.base
.indentation
= 0;
665 ctx
.left
= (int)size
;
667 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);