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
;
52 void (*dump_printf
)(struct dump_ctx
*ctx
, const char *format
, ...);
56 dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
61 _debug_vprintf(format
, ap
);
73 ctx
->dump_printf( ctx
, "%u", e
);
75 ctx
->dump_printf( ctx
, "%s", enums
[e
] );
78 #define EOL() ctx->dump_printf( ctx, "\n" )
79 #define TXT(S) ctx->dump_printf( ctx, "%s", S )
80 #define CHR(C) ctx->dump_printf( ctx, "%c", C )
81 #define UIX(I) ctx->dump_printf( ctx, "0x%x", I )
82 #define UID(I) ctx->dump_printf( ctx, "%u", I )
83 #define INSTID(I) ctx->dump_printf( ctx, "% 3u", I )
84 #define SID(I) ctx->dump_printf( ctx, "%d", I )
85 #define FLT(F) ctx->dump_printf( ctx, "%10.4f", F )
86 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
89 tgsi_swizzle_names
[4] =
100 const struct tgsi_full_src_register
*src
)
102 ENM(src
->Register
.File
, tgsi_file_names
);
103 if (src
->Register
.Dimension
) {
104 if (src
->Dimension
.Indirect
) {
106 ENM( src
->DimIndirect
.File
, tgsi_file_names
);
108 SID( src
->DimIndirect
.Index
);
110 ENM( src
->DimIndirect
.SwizzleX
, tgsi_swizzle_names
);
111 if (src
->Dimension
.Index
!= 0) {
112 if (src
->Dimension
.Index
> 0)
114 SID( src
->Dimension
.Index
);
119 SID(src
->Dimension
.Index
);
123 if (src
->Register
.Indirect
) {
125 ENM( src
->Indirect
.File
, tgsi_file_names
);
127 SID( src
->Indirect
.Index
);
129 ENM( src
->Indirect
.SwizzleX
, tgsi_swizzle_names
);
130 if (src
->Register
.Index
!= 0) {
131 if (src
->Register
.Index
> 0)
133 SID( src
->Register
.Index
);
138 SID( src
->Register
.Index
);
146 struct dump_ctx
*ctx
,
147 const struct tgsi_full_dst_register
*dst
)
149 ENM(dst
->Register
.File
, tgsi_file_names
);
150 if (dst
->Register
.Dimension
) {
151 if (dst
->Dimension
.Indirect
) {
153 ENM( dst
->DimIndirect
.File
, tgsi_file_names
);
155 SID( dst
->DimIndirect
.Index
);
157 ENM( dst
->DimIndirect
.SwizzleX
, tgsi_swizzle_names
);
158 if (dst
->Dimension
.Index
!= 0) {
159 if (dst
->Dimension
.Index
> 0)
161 SID( dst
->Dimension
.Index
);
166 SID(dst
->Dimension
.Index
);
170 if (dst
->Register
.Indirect
) {
172 ENM( dst
->Indirect
.File
, tgsi_file_names
);
174 SID( dst
->Indirect
.Index
);
176 ENM( dst
->Indirect
.SwizzleX
, tgsi_swizzle_names
);
177 if (dst
->Register
.Index
!= 0) {
178 if (dst
->Register
.Index
> 0)
180 SID( dst
->Register
.Index
);
185 SID( dst
->Register
.Index
);
191 struct dump_ctx
*ctx
,
194 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
196 if (writemask
& TGSI_WRITEMASK_X
)
198 if (writemask
& TGSI_WRITEMASK_Y
)
200 if (writemask
& TGSI_WRITEMASK_Z
)
202 if (writemask
& TGSI_WRITEMASK_W
)
208 dump_imm_data(struct tgsi_iterate_context
*iter
,
209 union tgsi_immediate_data
*data
,
213 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
218 assert( num_tokens
<= 4 );
219 for (i
= 0; i
< num_tokens
; i
++) {
221 case TGSI_IMM_FLOAT32
:
222 FLT( data
[i
].Float
);
224 case TGSI_IMM_UINT32
:
234 if (i
< num_tokens
- 1)
242 struct tgsi_iterate_context
*iter
,
243 struct tgsi_full_declaration
*decl
)
245 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
249 ENM(decl
->Declaration
.File
, tgsi_file_names
);
251 /* all geometry shader inputs are two dimensional */
252 if (decl
->Declaration
.File
== TGSI_FILE_INPUT
&&
253 iter
->processor
.Processor
== TGSI_PROCESSOR_GEOMETRY
) {
257 if (decl
->Declaration
.Dimension
) {
259 SID(decl
->Dim
.Index2D
);
264 SID(decl
->Range
.First
);
265 if (decl
->Range
.First
!= decl
->Range
.Last
) {
267 SID(decl
->Range
.Last
);
273 decl
->Declaration
.UsageMask
);
275 if (decl
->Declaration
.Local
)
278 if (decl
->Declaration
.Semantic
) {
280 ENM( decl
->Semantic
.Name
, tgsi_semantic_names
);
281 if (decl
->Semantic
.Index
!= 0 ||
282 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
284 UID( decl
->Semantic
.Index
);
289 if (decl
->Declaration
.File
== TGSI_FILE_RESOURCE
) {
291 ENM(decl
->Resource
.Resource
, tgsi_texture_names
);
292 if (decl
->Resource
.Writable
)
294 if (decl
->Resource
.Raw
)
298 if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER_VIEW
) {
300 ENM(decl
->SamplerView
.Resource
, tgsi_texture_names
);
302 if ((decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeY
) &&
303 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeZ
) &&
304 (decl
->SamplerView
.ReturnTypeX
== decl
->SamplerView
.ReturnTypeW
)) {
305 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_type_names
);
307 ENM(decl
->SamplerView
.ReturnTypeX
, tgsi_type_names
);
309 ENM(decl
->SamplerView
.ReturnTypeY
, tgsi_type_names
);
311 ENM(decl
->SamplerView
.ReturnTypeZ
, tgsi_type_names
);
313 ENM(decl
->SamplerView
.ReturnTypeW
, tgsi_type_names
);
317 if (decl
->Declaration
.Interpolate
) {
318 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
319 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
322 ENM( decl
->Interp
.Interpolate
, tgsi_interpolate_names
);
325 if (decl
->Interp
.Centroid
) {
329 if (decl
->Interp
.CylindricalWrap
) {
331 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_X
) {
334 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Y
) {
337 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_Z
) {
340 if (decl
->Interp
.CylindricalWrap
& TGSI_CYLINDRICAL_WRAP_W
) {
346 if (decl
->Declaration
.Invariant
) {
347 TXT( ", INVARIANT" );
351 if (decl
->Declaration
.File
== TGSI_FILE_IMMEDIATE_ARRAY
) {
353 char range_indent
[4];
357 if (decl
->Range
.Last
< 10)
358 range_indent
[0] = '\0';
359 else if (decl
->Range
.Last
< 100) {
360 range_indent
[0] = ' ';
361 range_indent
[1] = '\0';
362 } else if (decl
->Range
.Last
< 1000) {
363 range_indent
[0] = ' ';
364 range_indent
[1] = ' ';
365 range_indent
[2] = '\0';
367 range_indent
[0] = ' ';
368 range_indent
[1] = ' ';
369 range_indent
[2] = ' ';
370 range_indent
[3] = '\0';
373 dump_imm_data(iter
, decl
->ImmediateData
.u
,
374 4, TGSI_IMM_FLOAT32
);
375 for(i
= 1; i
<= decl
->Range
.Last
; ++i
) {
376 /* indent by strlen of:
377 * "DCL IMMX[0..1] {" */
381 dump_imm_data(iter
, decl
->ImmediateData
.u
+ i
,
382 4, TGSI_IMM_FLOAT32
);
394 tgsi_dump_declaration(
395 const struct tgsi_full_declaration
*decl
)
399 ctx
.dump_printf
= dump_ctx_printf
;
401 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
406 struct tgsi_iterate_context
*iter
,
407 struct tgsi_full_property
*prop
)
410 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
413 ENM(prop
->Property
.PropertyName
, tgsi_property_names
);
415 if (prop
->Property
.NrTokens
> 1)
418 for (i
= 0; i
< prop
->Property
.NrTokens
- 1; ++i
) {
419 switch (prop
->Property
.PropertyName
) {
420 case TGSI_PROPERTY_GS_INPUT_PRIM
:
421 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
422 ENM(prop
->u
[i
].Data
, tgsi_primitive_names
);
424 case TGSI_PROPERTY_FS_COORD_ORIGIN
:
425 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_origin_names
);
427 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
:
428 ENM(prop
->u
[i
].Data
, tgsi_fs_coord_pixel_center_names
);
431 SID( prop
->u
[i
].Data
);
434 if (i
< prop
->Property
.NrTokens
- 2)
442 void tgsi_dump_property(
443 const struct tgsi_full_property
*prop
)
447 ctx
.dump_printf
= dump_ctx_printf
;
449 iter_property( &ctx
.iter
, (struct tgsi_full_property
*)prop
);
454 struct tgsi_iterate_context
*iter
,
455 struct tgsi_full_immediate
*imm
)
457 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
462 ENM( imm
->Immediate
.DataType
, tgsi_immediate_type_names
);
464 dump_imm_data(iter
, imm
->u
, imm
->Immediate
.NrTokens
- 1,
465 imm
->Immediate
.DataType
);
474 const struct tgsi_full_immediate
*imm
)
478 ctx
.dump_printf
= dump_ctx_printf
;
480 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
485 struct tgsi_iterate_context
*iter
,
486 struct tgsi_full_instruction
*inst
)
488 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
489 uint instno
= ctx
->instno
++;
490 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
492 boolean first_reg
= TRUE
;
497 ctx
->indent
-= info
->pre_dedent
;
498 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
500 ctx
->indent
+= info
->post_indent
;
502 if (inst
->Instruction
.Predicate
) {
505 if (inst
->Predicate
.Negate
)
509 SID( inst
->Predicate
.Index
);
512 if (inst
->Predicate
.SwizzleX
!= TGSI_SWIZZLE_X
||
513 inst
->Predicate
.SwizzleY
!= TGSI_SWIZZLE_Y
||
514 inst
->Predicate
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
515 inst
->Predicate
.SwizzleW
!= TGSI_SWIZZLE_W
) {
517 ENM( inst
->Predicate
.SwizzleX
, tgsi_swizzle_names
);
518 ENM( inst
->Predicate
.SwizzleY
, tgsi_swizzle_names
);
519 ENM( inst
->Predicate
.SwizzleZ
, tgsi_swizzle_names
);
520 ENM( inst
->Predicate
.SwizzleW
, tgsi_swizzle_names
);
526 TXT( info
->mnemonic
);
528 switch (inst
->Instruction
.Saturate
) {
531 case TGSI_SAT_ZERO_ONE
:
534 case TGSI_SAT_MINUS_PLUS_ONE
:
541 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
542 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
548 _dump_register_dst( ctx
, dst
);
549 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
554 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
555 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
561 if (src
->Register
.Negate
)
563 if (src
->Register
.Absolute
)
566 _dump_register_src(ctx
, src
);
568 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
569 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
570 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
571 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
573 ENM( src
->Register
.SwizzleX
, tgsi_swizzle_names
);
574 ENM( src
->Register
.SwizzleY
, tgsi_swizzle_names
);
575 ENM( src
->Register
.SwizzleZ
, tgsi_swizzle_names
);
576 ENM( src
->Register
.SwizzleW
, tgsi_swizzle_names
);
579 if (src
->Register
.Absolute
)
585 if (inst
->Instruction
.Texture
) {
587 ENM( inst
->Texture
.Texture
, tgsi_texture_names
);
588 for (i
= 0; i
< inst
->Texture
.NumOffsets
; i
++) {
590 ENM( inst
->TexOffsets
[i
].File
, tgsi_file_names
);
592 SID( inst
->TexOffsets
[i
].Index
);
595 ENM( inst
->TexOffsets
[i
].SwizzleX
, tgsi_swizzle_names
);
596 ENM( inst
->TexOffsets
[i
].SwizzleY
, tgsi_swizzle_names
);
597 ENM( inst
->TexOffsets
[i
].SwizzleZ
, tgsi_swizzle_names
);
601 switch (inst
->Instruction
.Opcode
) {
603 case TGSI_OPCODE_ELSE
:
604 case TGSI_OPCODE_BGNLOOP
:
605 case TGSI_OPCODE_ENDLOOP
:
606 case TGSI_OPCODE_CAL
:
608 UID( inst
->Label
.Label
);
612 /* update indentation */
613 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
614 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
615 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
616 ctx
->indentation
+= indent_spaces
;
625 tgsi_dump_instruction(
626 const struct tgsi_full_instruction
*inst
,
634 ctx
.dump_printf
= dump_ctx_printf
;
637 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
642 struct tgsi_iterate_context
*iter
)
644 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
645 ENM( iter
->processor
.Processor
, tgsi_processor_type_names
);
652 const struct tgsi_token
*tokens
,
657 ctx
.iter
.prolog
= prolog
;
658 ctx
.iter
.iterate_instruction
= iter_instruction
;
659 ctx
.iter
.iterate_declaration
= iter_declaration
;
660 ctx
.iter
.iterate_immediate
= iter_immediate
;
661 ctx
.iter
.iterate_property
= iter_property
;
662 ctx
.iter
.epilog
= NULL
;
667 ctx
.dump_printf
= dump_ctx_printf
;
670 tgsi_iterate_shader( tokens
, &ctx
.iter
);
675 struct dump_ctx base
;
682 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
684 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
689 va_start(ap
, format
);
690 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
693 /* Some complicated logic needed to handle the return value of
697 written
= MIN2(sctx
->left
, written
);
698 sctx
->ptr
+= written
;
699 sctx
->left
-= written
;
706 const struct tgsi_token
*tokens
,
711 struct str_dump_ctx ctx
;
713 ctx
.base
.iter
.prolog
= prolog
;
714 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
715 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
716 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
717 ctx
.base
.iter
.iterate_property
= iter_property
;
718 ctx
.base
.iter
.epilog
= NULL
;
723 ctx
.base
.dump_printf
= &str_dump_ctx_printf
;
724 ctx
.base
.indentation
= 0;
729 ctx
.left
= (int)size
;
731 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);