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
] =
106 static const char *interpolate_names
[] =
113 static const char *semantic_names
[] =
125 static const char *immediate_type_names
[] =
130 static const char *swizzle_names
[] =
138 static const char *texture_names
[] =
151 static const char *extswizzle_names
[] =
161 static const char *modulate_names
[TGSI_MODULATE_COUNT
] =
174 struct dump_ctx
*ctx
,
179 ENM( file
, file_names
);
191 struct dump_ctx
*ctx
,
198 ENM( file
, file_names
);
200 ENM( ind_file
, file_names
);
204 ENM( ind_swizzle
, swizzle_names
);
215 struct dump_ctx
*ctx
,
218 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
220 if (writemask
& TGSI_WRITEMASK_X
)
222 if (writemask
& TGSI_WRITEMASK_Y
)
224 if (writemask
& TGSI_WRITEMASK_Z
)
226 if (writemask
& TGSI_WRITEMASK_W
)
233 struct tgsi_iterate_context
*iter
,
234 struct tgsi_full_declaration
*decl
)
236 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
238 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
239 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
245 decl
->Declaration
.File
,
246 decl
->DeclarationRange
.First
,
247 decl
->DeclarationRange
.Last
);
250 decl
->Declaration
.UsageMask
);
252 if (decl
->Declaration
.Semantic
) {
254 ENM( decl
->Semantic
.SemanticName
, semantic_names
);
255 if (decl
->Semantic
.SemanticIndex
!= 0 ||
256 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
) {
258 UID( decl
->Semantic
.SemanticIndex
);
263 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
264 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
267 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
270 if (decl
->Declaration
.Centroid
) {
274 if (decl
->Declaration
.Invariant
) {
275 TXT( ", INVARIANT" );
284 tgsi_dump_declaration(
285 const struct tgsi_full_declaration
*decl
)
289 ctx
.printf
= dump_ctx_printf
;
291 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
296 struct tgsi_iterate_context
*iter
,
297 struct tgsi_full_immediate
*imm
)
299 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
304 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
308 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
309 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
310 switch (imm
->Immediate
.DataType
) {
311 case TGSI_IMM_FLOAT32
:
312 FLT( imm
->u
[i
].Float
);
318 if (i
< imm
->Immediate
.NrTokens
- 2)
330 const struct tgsi_full_immediate
*imm
)
334 ctx
.printf
= dump_ctx_printf
;
336 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
341 struct tgsi_iterate_context
*iter
,
342 struct tgsi_full_instruction
*inst
)
344 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
345 uint instno
= ctx
->instno
++;
346 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
348 boolean first_reg
= TRUE
;
353 ctx
->indent
-= info
->pre_dedent
;
354 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
356 ctx
->indent
+= info
->post_indent
;
358 TXT( info
->mnemonic
);
360 switch (inst
->Instruction
.Saturate
) {
363 case TGSI_SAT_ZERO_ONE
:
366 case TGSI_SAT_MINUS_PLUS_ONE
:
373 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
374 const struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
380 if (dst
->DstRegister
.Indirect
) {
383 dst
->DstRegister
.File
,
384 dst
->DstRegister
.Index
,
385 dst
->DstRegisterInd
.File
,
386 dst
->DstRegisterInd
.Index
,
387 dst
->DstRegisterInd
.SwizzleX
);
392 dst
->DstRegister
.File
,
393 dst
->DstRegister
.Index
,
394 dst
->DstRegister
.Index
);
396 ENM( dst
->DstRegisterExtModulate
.Modulate
, modulate_names
);
397 _dump_writemask( ctx
, dst
->DstRegister
.WriteMask
);
402 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
403 const struct tgsi_full_src_register
*src
= &inst
->FullSrcRegisters
[i
];
409 if (src
->SrcRegisterExtMod
.Negate
)
411 if (src
->SrcRegisterExtMod
.Absolute
)
413 if (src
->SrcRegisterExtMod
.Scale2X
)
415 if (src
->SrcRegisterExtMod
.Bias
)
417 if (src
->SrcRegisterExtMod
.Complement
)
419 if (src
->SrcRegister
.Negate
)
422 if (src
->SrcRegister
.Indirect
) {
425 src
->SrcRegister
.File
,
426 src
->SrcRegister
.Index
,
427 src
->SrcRegisterInd
.File
,
428 src
->SrcRegisterInd
.Index
,
429 src
->SrcRegisterInd
.SwizzleX
);
434 src
->SrcRegister
.File
,
435 src
->SrcRegister
.Index
,
436 src
->SrcRegister
.Index
);
439 if (src
->SrcRegister
.SwizzleX
!= TGSI_SWIZZLE_X
||
440 src
->SrcRegister
.SwizzleY
!= TGSI_SWIZZLE_Y
||
441 src
->SrcRegister
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
442 src
->SrcRegister
.SwizzleW
!= TGSI_SWIZZLE_W
) {
444 ENM( src
->SrcRegister
.SwizzleX
, swizzle_names
);
445 ENM( src
->SrcRegister
.SwizzleY
, swizzle_names
);
446 ENM( src
->SrcRegister
.SwizzleZ
, swizzle_names
);
447 ENM( src
->SrcRegister
.SwizzleW
, swizzle_names
);
449 if (src
->SrcRegisterExtSwz
.ExtSwizzleX
!= TGSI_EXTSWIZZLE_X
||
450 src
->SrcRegisterExtSwz
.ExtSwizzleY
!= TGSI_EXTSWIZZLE_Y
||
451 src
->SrcRegisterExtSwz
.ExtSwizzleZ
!= TGSI_EXTSWIZZLE_Z
||
452 src
->SrcRegisterExtSwz
.ExtSwizzleW
!= TGSI_EXTSWIZZLE_W
) {
454 if (src
->SrcRegisterExtSwz
.NegateX
)
456 ENM( src
->SrcRegisterExtSwz
.ExtSwizzleX
, extswizzle_names
);
457 if (src
->SrcRegisterExtSwz
.NegateY
)
459 ENM( src
->SrcRegisterExtSwz
.ExtSwizzleY
, extswizzle_names
);
460 if (src
->SrcRegisterExtSwz
.NegateZ
)
462 ENM( src
->SrcRegisterExtSwz
.ExtSwizzleZ
, extswizzle_names
);
463 if (src
->SrcRegisterExtSwz
.NegateW
)
465 ENM( src
->SrcRegisterExtSwz
.ExtSwizzleW
, extswizzle_names
);
468 if (src
->SrcRegisterExtMod
.Complement
)
470 if (src
->SrcRegisterExtMod
.Bias
)
472 if (src
->SrcRegisterExtMod
.Scale2X
)
474 if (src
->SrcRegisterExtMod
.Absolute
)
476 if (src
->SrcRegisterExtMod
.Negate
)
482 if (inst
->InstructionExtTexture
.Texture
!= TGSI_TEXTURE_UNKNOWN
) {
484 ENM( inst
->InstructionExtTexture
.Texture
, texture_names
);
487 switch (inst
->Instruction
.Opcode
) {
489 case TGSI_OPCODE_ELSE
:
490 case TGSI_OPCODE_BGNLOOP
:
491 case TGSI_OPCODE_ENDLOOP
:
492 case TGSI_OPCODE_CAL
:
494 UID( inst
->InstructionExtLabel
.Label
);
498 /* update indentation */
499 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
500 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
501 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
502 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
503 ctx
->indentation
+= indent_spaces
;
512 tgsi_dump_instruction(
513 const struct tgsi_full_instruction
*inst
,
520 ctx
.printf
= dump_ctx_printf
;
523 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
528 struct tgsi_iterate_context
*iter
)
530 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
531 ENM( iter
->processor
.Processor
, processor_type_names
);
532 UID( iter
->version
.MajorVersion
);
534 UID( iter
->version
.MinorVersion
);
541 const struct tgsi_token
*tokens
,
546 ctx
.iter
.prolog
= prolog
;
547 ctx
.iter
.iterate_instruction
= iter_instruction
;
548 ctx
.iter
.iterate_declaration
= iter_declaration
;
549 ctx
.iter
.iterate_immediate
= iter_immediate
;
550 ctx
.iter
.epilog
= NULL
;
554 ctx
.printf
= dump_ctx_printf
;
557 tgsi_iterate_shader( tokens
, &ctx
.iter
);
562 struct dump_ctx base
;
569 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
571 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
576 va_start(ap
, format
);
577 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
580 /* Some complicated logic needed to handle the return value of
584 written
= MIN2(sctx
->left
, written
);
585 sctx
->ptr
+= written
;
586 sctx
->left
-= written
;
593 const struct tgsi_token
*tokens
,
598 struct str_dump_ctx ctx
;
600 ctx
.base
.iter
.prolog
= prolog
;
601 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
602 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
603 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
604 ctx
.base
.iter
.epilog
= NULL
;
608 ctx
.base
.printf
= &str_dump_ctx_printf
;
609 ctx
.base
.indentation
= 0;
614 ctx
.left
= (int)size
;
616 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);