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
,
210 ENM( file
, file_names
);
212 ENM( ind_file
, file_names
);
216 ENM( ind_swizzle
, swizzle_names
);
227 struct dump_ctx
*ctx
,
230 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
232 if (writemask
& TGSI_WRITEMASK_X
)
234 if (writemask
& TGSI_WRITEMASK_Y
)
236 if (writemask
& TGSI_WRITEMASK_Z
)
238 if (writemask
& TGSI_WRITEMASK_W
)
245 struct tgsi_iterate_context
*iter
,
246 struct tgsi_full_declaration
*decl
)
248 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
250 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
251 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
257 decl
->Declaration
.File
,
262 decl
->Declaration
.UsageMask
);
264 if (decl
->Declaration
.Semantic
) {
266 ENM( decl
->Semantic
.Name
, semantic_names
);
267 if (decl
->Semantic
.Index
!= 0 ||
268 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
270 UID( decl
->Semantic
.Index
);
275 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
276 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
279 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
282 if (decl
->Declaration
.Centroid
) {
286 if (decl
->Declaration
.Invariant
) {
287 TXT( ", INVARIANT" );
296 tgsi_dump_declaration(
297 const struct tgsi_full_declaration
*decl
)
301 ctx
.printf
= dump_ctx_printf
;
303 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
308 struct tgsi_iterate_context
*iter
,
309 struct tgsi_full_property
*prop
)
312 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
314 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
317 ENM(prop
->Property
.PropertyName
, property_names
);
319 if (prop
->Property
.NrTokens
> 1)
322 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
323 switch (prop
->Property
.PropertyName
) {
324 case TGSI_PROPERTY_GS_INPUT_PRIM
:
325 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
326 ENM(prop
->u
[i
].Data
, primitive_names
);
329 SID( prop
->u
[i
].Data
);
332 if (i
< prop
->Property
.NrTokens
- 2)
340 void tgsi_dump_property(
341 const struct tgsi_full_property
*prop
)
345 ctx
.printf
= dump_ctx_printf
;
347 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
352 struct tgsi_iterate_context
*iter
,
353 struct tgsi_full_immediate
*imm
)
355 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
360 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
364 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
365 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
366 switch (imm
->Immediate
.DataType
) {
367 case TGSI_IMM_FLOAT32
:
368 FLT( imm
->u
[i
].Float
);
374 if (i
< imm
->Immediate
.NrTokens
- 2)
386 const struct tgsi_full_immediate
*imm
)
390 ctx
.printf
= dump_ctx_printf
;
392 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
397 struct tgsi_iterate_context
*iter
,
398 struct tgsi_full_instruction
*inst
)
400 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
401 uint instno
= ctx
->instno
++;
402 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
404 boolean first_reg
= TRUE
;
409 ctx
->indent
-= info
->pre_dedent
;
410 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
412 ctx
->indent
+= info
->post_indent
;
414 TXT( info
->mnemonic
);
416 switch (inst
->Instruction
.Saturate
) {
419 case TGSI_SAT_ZERO_ONE
:
422 case TGSI_SAT_MINUS_PLUS_ONE
:
429 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
430 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
436 if (dst
->Register
.Indirect
) {
443 dst
->Indirect
.SwizzleX
);
450 dst
->Register
.Index
);
452 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
457 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
458 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
464 if (src
->Register
.Negate
)
466 if (src
->Register
.Absolute
)
469 if (src
->Register
.Indirect
) {
476 src
->Indirect
.SwizzleX
);
483 src
->Register
.Index
);
486 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
487 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
488 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
489 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
491 ENM( src
->Register
.SwizzleX
, swizzle_names
);
492 ENM( src
->Register
.SwizzleY
, swizzle_names
);
493 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
494 ENM( src
->Register
.SwizzleW
, swizzle_names
);
497 if (src
->Register
.Absolute
)
499 if (src
->Register
.Negate
)
505 if (inst
->Instruction
.Texture
) {
507 ENM( inst
->Texture
.Texture
, texture_names
);
510 switch (inst
->Instruction
.Opcode
) {
512 case TGSI_OPCODE_ELSE
:
513 case TGSI_OPCODE_BGNLOOP
:
514 case TGSI_OPCODE_ENDLOOP
:
515 case TGSI_OPCODE_CAL
:
517 UID( inst
->Label
.Label
);
521 /* update indentation */
522 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
523 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
524 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
525 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
526 ctx
->indentation
+= indent_spaces
;
535 tgsi_dump_instruction(
536 const struct tgsi_full_instruction
*inst
,
543 ctx
.printf
= dump_ctx_printf
;
546 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
551 struct tgsi_iterate_context
*iter
)
553 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
554 ENM( iter
->processor
.Processor
, processor_type_names
);
561 const struct tgsi_token
*tokens
,
566 ctx
.iter
.prolog
= prolog
;
567 ctx
.iter
.iterate_instruction
= iter_instruction
;
568 ctx
.iter
.iterate_declaration
= iter_declaration
;
569 ctx
.iter
.iterate_immediate
= iter_immediate
;
570 ctx
.iter
.iterate_property
= iter_property
;
571 ctx
.iter
.epilog
= NULL
;
575 ctx
.printf
= dump_ctx_printf
;
578 tgsi_iterate_shader( tokens
, &ctx
.iter
);
583 struct dump_ctx base
;
590 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
592 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
597 va_start(ap
, format
);
598 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
601 /* Some complicated logic needed to handle the return value of
605 written
= MIN2(sctx
->left
, written
);
606 sctx
->ptr
+= written
;
607 sctx
->left
-= written
;
614 const struct tgsi_token
*tokens
,
619 struct str_dump_ctx ctx
;
621 ctx
.base
.iter
.prolog
= prolog
;
622 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
623 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
624 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
625 ctx
.base
.iter
.iterate_property
= iter_property
;
626 ctx
.base
.iter
.epilog
= NULL
;
630 ctx
.base
.printf
= &str_dump_ctx_printf
;
631 ctx
.base
.indentation
= 0;
636 ctx
.left
= (int)size
;
638 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);