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"
35 #include "tgsi_strings.h"
38 /** Number of spaces to indent for IF/LOOP/etc */
39 static const int indent_spaces
= 3;
44 struct tgsi_iterate_context iter
;
51 void (*printf
)(struct dump_ctx
*ctx
, const char *format
, ...);
55 dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
60 _debug_vprintf(format
, ap
);
72 ctx
->printf( ctx
, "%u", e
);
74 ctx
->printf( ctx
, "%s", enums
[e
] );
77 #define EOL() ctx->printf( ctx, "\n" )
78 #define TXT(S) ctx->printf( ctx, "%s", S )
79 #define CHR(C) ctx->printf( ctx, "%c", C )
80 #define UIX(I) ctx->printf( ctx, "0x%x", I )
81 #define UID(I) ctx->printf( ctx, "%u", I )
82 #define INSTID(I) ctx->printf( ctx, "% 3u", I )
83 #define SID(I) ctx->printf( ctx, "%d", I )
84 #define FLT(F) ctx->printf( ctx, "%10.4f", F )
85 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
88 tgsi_swizzle_names
[4] =
99 const struct tgsi_full_src_register
*src
)
101 ENM(src
->Register
.File
, tgsi_file_names
);
102 if (src
->Register
.Dimension
) {
103 if (src
->Dimension
.Indirect
) {
105 ENM( src
->DimIndirect
.File
, tgsi_file_names
);
107 SID( src
->DimIndirect
.Index
);
109 ENM( src
->DimIndirect
.SwizzleX
, tgsi_swizzle_names
);
110 if (src
->Dimension
.Index
!= 0) {
111 if (src
->Dimension
.Index
> 0)
113 SID( src
->Dimension
.Index
);
118 SID(src
->Dimension
.Index
);
122 if (src
->Register
.Indirect
) {
124 ENM( src
->Indirect
.File
, tgsi_file_names
);
126 SID( src
->Indirect
.Index
);
128 ENM( src
->Indirect
.SwizzleX
, tgsi_swizzle_names
);
129 if (src
->Register
.Index
!= 0) {
130 if (src
->Register
.Index
> 0)
132 SID( src
->Register
.Index
);
137 SID( src
->Register
.Index
);
145 struct dump_ctx
*ctx
,
146 const struct tgsi_full_dst_register
*dst
)
148 ENM(dst
->Register
.File
, tgsi_file_names
);
149 if (dst
->Register
.Dimension
) {
150 if (dst
->Dimension
.Indirect
) {
152 ENM( dst
->DimIndirect
.File
, tgsi_file_names
);
154 SID( dst
->DimIndirect
.Index
);
156 ENM( dst
->DimIndirect
.SwizzleX
, tgsi_swizzle_names
);
157 if (dst
->Dimension
.Index
!= 0) {
158 if (dst
->Dimension
.Index
> 0)
160 SID( dst
->Dimension
.Index
);
165 SID(dst
->Dimension
.Index
);
169 if (dst
->Register
.Indirect
) {
171 ENM( dst
->Indirect
.File
, tgsi_file_names
);
173 SID( dst
->Indirect
.Index
);
175 ENM( dst
->Indirect
.SwizzleX
, tgsi_swizzle_names
);
176 if (dst
->Register
.Index
!= 0) {
177 if (dst
->Register
.Index
> 0)
179 SID( dst
->Register
.Index
);
184 SID( dst
->Register
.Index
);
190 struct dump_ctx
*ctx
,
193 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
195 if (writemask
& TGSI_WRITEMASK_X
)
197 if (writemask
& TGSI_WRITEMASK_Y
)
199 if (writemask
& TGSI_WRITEMASK_Z
)
201 if (writemask
& TGSI_WRITEMASK_W
)
207 dump_imm_data(struct tgsi_iterate_context
*iter
,
208 union tgsi_immediate_data
*data
,
212 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
217 assert( num_tokens
<= 4 );
218 for (i
= 0; i
< num_tokens
; i
++) {
220 case TGSI_IMM_FLOAT32
:
221 FLT( data
[i
].Float
);
223 case TGSI_IMM_UINT32
:
233 if (i
< num_tokens
- 1)
241 struct tgsi_iterate_context
*iter
,
242 struct tgsi_full_declaration
*decl
)
244 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
248 ENM(decl
->Declaration
.File
, tgsi_file_names
);
250 /* all geometry shader inputs are two dimensional */
251 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
252 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
256 if (decl
->Declaration
.Dimension
) {
258 SID(decl
->Dim
.Index2D
);
263 SID(decl
->Range
.First
);
264 if (decl
->Range
.First
!= decl
->Range
.Last
) {
266 SID(decl
->Range
.Last
);
272 decl
->Declaration
.UsageMask
);
274 if (decl
->Declaration
.Semantic
) {
276 ENM( decl
->Semantic
.Name
, tgsi_semantic_names
);
277 if (decl
->Semantic
.Index
!= 0 ||
278 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
280 UID( decl
->Semantic
.Index
);
285 if (decl
->Declaration
.File
== TGSI_FILE_RESOURCE
) {
287 ENM(decl
->Resource
.Resource
, tgsi_texture_names
);
289 if ((decl
->Resource
.ReturnTypeX
== decl
->Resource
.ReturnTypeY
) &&
290 (decl
->Resource
.ReturnTypeX
== decl
->Resource
.ReturnTypeZ
) &&
291 (decl
->Resource
.ReturnTypeX
== decl
->Resource
.ReturnTypeW
)) {
292 ENM(decl
->Resource
.ReturnTypeX
, tgsi_type_names
);
294 ENM(decl
->Resource
.ReturnTypeX
, tgsi_type_names
);
296 ENM(decl
->Resource
.ReturnTypeY
, tgsi_type_names
);
298 ENM(decl
->Resource
.ReturnTypeZ
, tgsi_type_names
);
300 ENM(decl
->Resource
.ReturnTypeW
, tgsi_type_names
);
305 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
306 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
309 ENM( decl
->Declaration
.Interpolate
, tgsi_interpolate_names
);
312 if (decl
->Declaration
.Centroid
) {
316 if (decl
->Declaration
.Invariant
) {
317 TXT( ", INVARIANT" );
320 if (decl
->Declaration
.CylindricalWrap
) {
322 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
325 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
328 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
331 if (decl
->Declaration
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
336 if (decl
->Declaration
.File
== TGSI_FILE_IMMEDIATE_ARRAY
) {
338 char range_indent
[4];
342 if (decl
->Range
.Last
< 10)
343 range_indent
[0] = '\0';
344 else if (decl
->Range
.Last
< 100) {
345 range_indent
[0] = ' ';
346 range_indent
[1] = '\0';
347 } else if (decl
->Range
.Last
< 1000) {
348 range_indent
[0] = ' ';
349 range_indent
[1] = ' ';
350 range_indent
[2] = '\0';
352 range_indent
[0] = ' ';
353 range_indent
[1] = ' ';
354 range_indent
[2] = ' ';
355 range_indent
[3] = '\0';
358 dump_imm_data(iter
, decl
->ImmediateData
.u
,
359 4, TGSI_IMM_FLOAT32
);
360 for(i
= 1; i
<= decl
->Range
.Last
; ++i
) {
361 /* indent by strlen of:
362 * "DCL IMMX[0..1] {" */
366 dump_imm_data(iter
, decl
->ImmediateData
.u
+ i
,
367 4, TGSI_IMM_FLOAT32
);
379 tgsi_dump_declaration(
380 const struct tgsi_full_declaration
*decl
)
384 ctx
.printf
= dump_ctx_printf
;
386 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
391 struct tgsi_iterate_context
*iter
,
392 struct tgsi_full_property
*prop
)
395 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
398 ENM(prop
->Property
.PropertyName
, tgsi_property_names
);
400 if (prop
->Property
.NrTokens
> 1)
403 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
404 switch (prop
->Property
.PropertyName
) {
405 case TGSI_PROPERTY_GS_INPUT_PRIM
:
406 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
407 ENM(prop
->u
[i
].Data
, tgsi_primitive_names
);
409 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
410 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_origin_names
);
412 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
413 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_pixel_center_names
);
416 SID( prop
->u
[i
].Data
);
419 if (i
< prop
->Property
.NrTokens
- 2)
427 void tgsi_dump_property(
428 const struct tgsi_full_property
*prop
)
432 ctx
.printf
= dump_ctx_printf
;
434 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
439 struct tgsi_iterate_context
*iter
,
440 struct tgsi_full_immediate
*imm
)
442 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
445 ENM( imm
->Immediate
.DataType
, tgsi_immediate_type_names
);
447 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
448 imm
->Immediate
.DataType
);
457 const struct tgsi_full_immediate
*imm
)
461 ctx
.printf
= dump_ctx_printf
;
463 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
468 struct tgsi_iterate_context
*iter
,
469 struct tgsi_full_instruction
*inst
)
471 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
472 uint instno
= ctx
->instno
++;
473 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
475 boolean first_reg
= TRUE
;
480 ctx
->indent
-= info
->pre_dedent
;
481 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
483 ctx
->indent
+= info
->post_indent
;
485 if (inst
->Instruction
.Predicate
) {
488 if (inst
->Predicate
.Negate
)
492 SID( inst
->Predicate
.Index
);
495 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
496 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
497 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
498 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
500 ENM( inst
->Predicate
.SwizzleX
, tgsi_swizzle_names
);
501 ENM( inst
->Predicate
.SwizzleY
, tgsi_swizzle_names
);
502 ENM( inst
->Predicate
.SwizzleZ
, tgsi_swizzle_names
);
503 ENM( inst
->Predicate
.SwizzleW
, tgsi_swizzle_names
);
509 TXT( info
->mnemonic
);
511 switch (inst
->Instruction
.Saturate
) {
514 case TGSI_SAT_ZERO_ONE
:
517 case TGSI_SAT_MINUS_PLUS_ONE
:
524 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
525 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
531 _dump_register_dst( ctx
, dst
);
532 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
537 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
538 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
544 if (src
->Register
.Negate
)
546 if (src
->Register
.Absolute
)
549 _dump_register_src(ctx
, src
);
551 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
552 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
553 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
554 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
556 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
557 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
558 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
559 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
562 if (src
->Register
.Absolute
)
568 if (inst
->Instruction
.Texture
) {
570 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
571 for (i
= 0; i
< inst
->Texture
.NumOffsets
; i
++) {
573 ENM( inst
->TexOffsets
[i
].File
, tgsi_file_names
);
575 SID( inst
->TexOffsets
[i
].Index
);
578 ENM( inst
->TexOffsets
[i
].SwizzleX
, tgsi_swizzle_names
);
579 ENM( inst
->TexOffsets
[i
].SwizzleY
, tgsi_swizzle_names
);
580 ENM( inst
->TexOffsets
[i
].SwizzleZ
, tgsi_swizzle_names
);
584 switch (inst
->Instruction
.Opcode
) {
586 case TGSI_OPCODE_ELSE
:
587 case TGSI_OPCODE_BGNLOOP
:
588 case TGSI_OPCODE_ENDLOOP
:
589 case TGSI_OPCODE_CAL
:
591 UID( inst
->Label
.Label
);
595 /* update indentation */
596 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
597 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
598 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
599 ctx
->indentation
+= indent_spaces
;
608 tgsi_dump_instruction(
609 const struct tgsi_full_instruction
*inst
,
616 ctx
.printf
= dump_ctx_printf
;
619 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
624 struct tgsi_iterate_context
*iter
)
626 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
627 ENM( iter
->processor
.Processor
, tgsi_processor_type_names
);
634 const struct tgsi_token
*tokens
,
639 ctx
.iter
.prolog
= prolog
;
640 ctx
.iter
.iterate_instruction
= iter_instruction
;
641 ctx
.iter
.iterate_declaration
= iter_declaration
;
642 ctx
.iter
.iterate_immediate
= iter_immediate
;
643 ctx
.iter
.iterate_property
= iter_property
;
644 ctx
.iter
.epilog
= NULL
;
648 ctx
.printf
= dump_ctx_printf
;
651 tgsi_iterate_shader( tokens
, &ctx
.iter
);
656 struct dump_ctx base
;
663 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
665 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
670 va_start(ap
, format
);
671 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
674 /* Some complicated logic needed to handle the return value of
678 written
= MIN2(sctx
->left
, written
);
679 sctx
->ptr
+= written
;
680 sctx
->left
-= written
;
687 const struct tgsi_token
*tokens
,
692 struct str_dump_ctx ctx
;
694 ctx
.base
.iter
.prolog
= prolog
;
695 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
696 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
697 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
698 ctx
.base
.iter
.iterate_property
= iter_property
;
699 ctx
.base
.iter
.epilog
= NULL
;
703 ctx
.base
.printf
= &str_dump_ctx_printf
;
704 ctx
.base
.indentation
= 0;
709 ctx
.left
= (int)size
;
711 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);