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",
164 "FS_COORD_PIXEL_CENTER"
167 static const char *primitive_names
[] =
181 static const char *fs_coord_origin_names
[] =
187 static const char *fs_coord_pixel_center_names
[] =
196 struct dump_ctx
*ctx
,
200 ENM( file
, file_names
);
210 struct dump_ctx
*ctx
,
211 const struct tgsi_full_src_register
*src
)
213 ENM(src
->Register
.File
, file_names
);
214 if (src
->Register
.Dimension
) {
216 SID(src
->Dimension
.Index
);
219 if (src
->Register
.Indirect
) {
221 ENM( src
->Indirect
.File
, file_names
);
223 SID( src
->Indirect
.Index
);
225 ENM( src
->Indirect
.SwizzleX
, swizzle_names
);
226 if (src
->Register
.Index
!= 0) {
227 if (src
->Register
.Index
> 0)
229 SID( src
->Register
.Index
);
234 SID( src
->Register
.Index
);
241 struct dump_ctx
*ctx
,
248 ENM( file
, file_names
);
250 ENM( ind_file
, file_names
);
254 ENM( ind_swizzle
, swizzle_names
);
265 struct dump_ctx
*ctx
,
268 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
270 if (writemask
& TGSI_WRITEMASK_X
)
272 if (writemask
& TGSI_WRITEMASK_Y
)
274 if (writemask
& TGSI_WRITEMASK_Z
)
276 if (writemask
& TGSI_WRITEMASK_W
)
283 struct tgsi_iterate_context
*iter
,
284 struct tgsi_full_declaration
*decl
)
286 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
288 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
289 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
293 ENM(decl
->Declaration
.File
, file_names
);
295 /* all geometry shader inputs are two dimensional */
296 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
297 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
301 if (decl
->Declaration
.Dimension
) {
303 SID(decl
->Dim
.Index2D
);
308 SID(decl
->Range
.First
);
309 if (decl
->Range
.First
!= decl
->Range
.Last
) {
311 SID(decl
->Range
.Last
);
317 decl
->Declaration
.UsageMask
);
319 if (decl
->Declaration
.Semantic
) {
321 ENM( decl
->Semantic
.Name
, semantic_names
);
322 if (decl
->Semantic
.Index
!= 0 ||
323 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
325 UID( decl
->Semantic
.Index
);
330 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
331 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
334 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
337 if (decl
->Declaration
.Centroid
) {
341 if (decl
->Declaration
.Invariant
) {
342 TXT( ", INVARIANT" );
345 if (decl
->Declaration
.CylindricalWrap
) {
347 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
350 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
353 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
356 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
367 tgsi_dump_declaration(
368 const struct tgsi_full_declaration
*decl
)
372 ctx
.printf
= dump_ctx_printf
;
374 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
379 struct tgsi_iterate_context
*iter
,
380 struct tgsi_full_property
*prop
)
383 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
385 assert(Elements(property_names
) == TGSI_PROPERTY_COUNT
);
388 ENM(prop
->Property
.PropertyName
, property_names
);
390 if (prop
->Property
.NrTokens
> 1)
393 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
394 switch (prop
->Property
.PropertyName
) {
395 case TGSI_PROPERTY_GS_INPUT_PRIM
:
396 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
397 ENM(prop
->u
[i
].Data
, primitive_names
);
399 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
400 ENM(prop
->u
[i
].Data
, fs_coord_origin_names
);
402 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
403 ENM(prop
->u
[i
].Data
, fs_coord_pixel_center_names
);
406 SID( prop
->u
[i
].Data
);
409 if (i
< prop
->Property
.NrTokens
- 2)
417 void tgsi_dump_property(
418 const struct tgsi_full_property
*prop
)
422 ctx
.printf
= dump_ctx_printf
;
424 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
429 struct tgsi_iterate_context
*iter
,
430 struct tgsi_full_immediate
*imm
)
432 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
437 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
441 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
442 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
443 switch (imm
->Immediate
.DataType
) {
444 case TGSI_IMM_FLOAT32
:
445 FLT( imm
->u
[i
].Float
);
447 case TGSI_IMM_UINT32
:
457 if (i
< imm
->Immediate
.NrTokens
- 2)
469 const struct tgsi_full_immediate
*imm
)
473 ctx
.printf
= dump_ctx_printf
;
475 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
480 struct tgsi_iterate_context
*iter
,
481 struct tgsi_full_instruction
*inst
)
483 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
484 uint instno
= ctx
->instno
++;
485 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
487 boolean first_reg
= TRUE
;
492 ctx
->indent
-= info
->pre_dedent
;
493 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
495 ctx
->indent
+= info
->post_indent
;
497 TXT( info
->mnemonic
);
499 switch (inst
->Instruction
.Saturate
) {
502 case TGSI_SAT_ZERO_ONE
:
505 case TGSI_SAT_MINUS_PLUS_ONE
:
512 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
513 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
519 if (dst
->Register
.Indirect
) {
526 dst
->Indirect
.SwizzleX
);
532 dst
->Register
.Index
);
534 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
539 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
540 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
546 if (src
->Register
.Negate
)
548 if (src
->Register
.Absolute
)
551 _dump_register_src(ctx
, src
);
553 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
554 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
555 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
556 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
558 ENM( src
->Register
.SwizzleX
, swizzle_names
);
559 ENM( src
->Register
.SwizzleY
, swizzle_names
);
560 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
561 ENM( src
->Register
.SwizzleW
, swizzle_names
);
564 if (src
->Register
.Absolute
)
570 if (inst
->Instruction
.Texture
) {
572 ENM( inst
->Texture
.Texture
, texture_names
);
575 switch (inst
->Instruction
.Opcode
) {
577 case TGSI_OPCODE_ELSE
:
578 case TGSI_OPCODE_BGNLOOP
:
579 case TGSI_OPCODE_ENDLOOP
:
580 case TGSI_OPCODE_CAL
:
582 UID( inst
->Label
.Label
);
586 /* update indentation */
587 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
588 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
589 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
590 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
591 ctx
->indentation
+= indent_spaces
;
600 tgsi_dump_instruction(
601 const struct tgsi_full_instruction
*inst
,
608 ctx
.printf
= dump_ctx_printf
;
611 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
616 struct tgsi_iterate_context
*iter
)
618 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
619 ENM( iter
->processor
.Processor
, processor_type_names
);
626 const struct tgsi_token
*tokens
,
631 ctx
.iter
.prolog
= prolog
;
632 ctx
.iter
.iterate_instruction
= iter_instruction
;
633 ctx
.iter
.iterate_declaration
= iter_declaration
;
634 ctx
.iter
.iterate_immediate
= iter_immediate
;
635 ctx
.iter
.iterate_property
= iter_property
;
636 ctx
.iter
.epilog
= NULL
;
640 ctx
.printf
= dump_ctx_printf
;
643 tgsi_iterate_shader( tokens
, &ctx
.iter
);
648 struct dump_ctx base
;
655 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
657 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
662 va_start(ap
, format
);
663 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
666 /* Some complicated logic needed to handle the return value of
670 written
= MIN2(sctx
->left
, written
);
671 sctx
->ptr
+= written
;
672 sctx
->left
-= written
;
679 const struct tgsi_token
*tokens
,
684 struct str_dump_ctx ctx
;
686 ctx
.base
.iter
.prolog
= prolog
;
687 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
688 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
689 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
690 ctx
.base
.iter
.iterate_property
= iter_property
;
691 ctx
.base
.iter
.epilog
= NULL
;
695 ctx
.base
.printf
= &str_dump_ctx_printf
;
696 ctx
.base
.indentation
= 0;
701 ctx
.left
= (int)size
;
703 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);