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
[] =
152 static const char *modulate_names
[TGSI_MODULATE_COUNT
] =
165 struct dump_ctx
*ctx
,
170 ENM( file
, file_names
);
182 struct dump_ctx
*ctx
,
189 ENM( file
, file_names
);
191 ENM( ind_file
, file_names
);
195 ENM( ind_swizzle
, swizzle_names
);
206 struct dump_ctx
*ctx
,
209 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
211 if (writemask
& TGSI_WRITEMASK_X
)
213 if (writemask
& TGSI_WRITEMASK_Y
)
215 if (writemask
& TGSI_WRITEMASK_Z
)
217 if (writemask
& TGSI_WRITEMASK_W
)
224 struct tgsi_iterate_context
*iter
,
225 struct tgsi_full_declaration
*decl
)
227 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
229 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
230 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
236 decl
->Declaration
.File
,
237 decl
->DeclarationRange
.First
,
238 decl
->DeclarationRange
.Last
);
241 decl
->Declaration
.UsageMask
);
243 if (decl
->Declaration
.Semantic
) {
245 ENM( decl
->Semantic
.SemanticName
, semantic_names
);
246 if (decl
->Semantic
.SemanticIndex
!= 0 ||
247 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
) {
249 UID( decl
->Semantic
.SemanticIndex
);
254 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
255 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
258 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
261 if (decl
->Declaration
.Centroid
) {
265 if (decl
->Declaration
.Invariant
) {
266 TXT( ", INVARIANT" );
275 tgsi_dump_declaration(
276 const struct tgsi_full_declaration
*decl
)
280 ctx
.printf
= dump_ctx_printf
;
282 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
287 struct tgsi_iterate_context
*iter
,
288 struct tgsi_full_immediate
*imm
)
290 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
295 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
299 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
300 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
301 switch (imm
->Immediate
.DataType
) {
302 case TGSI_IMM_FLOAT32
:
303 FLT( imm
->u
[i
].Float
);
309 if (i
< imm
->Immediate
.NrTokens
- 2)
321 const struct tgsi_full_immediate
*imm
)
325 ctx
.printf
= dump_ctx_printf
;
327 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
332 struct tgsi_iterate_context
*iter
,
333 struct tgsi_full_instruction
*inst
)
335 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
336 uint instno
= ctx
->instno
++;
337 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
339 boolean first_reg
= TRUE
;
344 ctx
->indent
-= info
->pre_dedent
;
345 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
347 ctx
->indent
+= info
->post_indent
;
349 TXT( info
->mnemonic
);
351 switch (inst
->Instruction
.Saturate
) {
354 case TGSI_SAT_ZERO_ONE
:
357 case TGSI_SAT_MINUS_PLUS_ONE
:
364 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
365 const struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
371 if (dst
->DstRegister
.Indirect
) {
374 dst
->DstRegister
.File
,
375 dst
->DstRegister
.Index
,
376 dst
->DstRegisterInd
.File
,
377 dst
->DstRegisterInd
.Index
,
378 dst
->DstRegisterInd
.SwizzleX
);
383 dst
->DstRegister
.File
,
384 dst
->DstRegister
.Index
,
385 dst
->DstRegister
.Index
);
387 ENM( dst
->DstRegisterExtModulate
.Modulate
, modulate_names
);
388 _dump_writemask( ctx
, dst
->DstRegister
.WriteMask
);
393 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
394 const struct tgsi_full_src_register
*src
= &inst
->FullSrcRegisters
[i
];
400 if (src
->SrcRegisterExtMod
.Negate
)
402 if (src
->SrcRegisterExtMod
.Absolute
)
404 if (src
->SrcRegisterExtMod
.Scale2X
)
406 if (src
->SrcRegisterExtMod
.Bias
)
408 if (src
->SrcRegisterExtMod
.Complement
)
410 if (src
->SrcRegister
.Negate
)
413 if (src
->SrcRegister
.Indirect
) {
416 src
->SrcRegister
.File
,
417 src
->SrcRegister
.Index
,
418 src
->SrcRegisterInd
.File
,
419 src
->SrcRegisterInd
.Index
,
420 src
->SrcRegisterInd
.SwizzleX
);
425 src
->SrcRegister
.File
,
426 src
->SrcRegister
.Index
,
427 src
->SrcRegister
.Index
);
430 if (src
->SrcRegister
.SwizzleX
!= TGSI_SWIZZLE_X
||
431 src
->SrcRegister
.SwizzleY
!= TGSI_SWIZZLE_Y
||
432 src
->SrcRegister
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
433 src
->SrcRegister
.SwizzleW
!= TGSI_SWIZZLE_W
) {
435 ENM( src
->SrcRegister
.SwizzleX
, swizzle_names
);
436 ENM( src
->SrcRegister
.SwizzleY
, swizzle_names
);
437 ENM( src
->SrcRegister
.SwizzleZ
, swizzle_names
);
438 ENM( src
->SrcRegister
.SwizzleW
, swizzle_names
);
441 if (src
->SrcRegisterExtMod
.Complement
)
443 if (src
->SrcRegisterExtMod
.Bias
)
445 if (src
->SrcRegisterExtMod
.Scale2X
)
447 if (src
->SrcRegisterExtMod
.Absolute
)
449 if (src
->SrcRegisterExtMod
.Negate
)
455 if (inst
->InstructionExtTexture
.Texture
!= TGSI_TEXTURE_UNKNOWN
) {
457 ENM( inst
->InstructionExtTexture
.Texture
, texture_names
);
460 switch (inst
->Instruction
.Opcode
) {
462 case TGSI_OPCODE_ELSE
:
463 case TGSI_OPCODE_BGNLOOP
:
464 case TGSI_OPCODE_ENDLOOP
:
465 case TGSI_OPCODE_CAL
:
467 UID( inst
->InstructionExtLabel
.Label
);
471 /* update indentation */
472 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
473 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
474 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
475 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
476 ctx
->indentation
+= indent_spaces
;
485 tgsi_dump_instruction(
486 const struct tgsi_full_instruction
*inst
,
493 ctx
.printf
= dump_ctx_printf
;
496 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
501 struct tgsi_iterate_context
*iter
)
503 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
504 ENM( iter
->processor
.Processor
, processor_type_names
);
505 UID( iter
->version
.MajorVersion
);
507 UID( iter
->version
.MinorVersion
);
514 const struct tgsi_token
*tokens
,
519 ctx
.iter
.prolog
= prolog
;
520 ctx
.iter
.iterate_instruction
= iter_instruction
;
521 ctx
.iter
.iterate_declaration
= iter_declaration
;
522 ctx
.iter
.iterate_immediate
= iter_immediate
;
523 ctx
.iter
.epilog
= NULL
;
527 ctx
.printf
= dump_ctx_printf
;
530 tgsi_iterate_shader( tokens
, &ctx
.iter
);
535 struct dump_ctx base
;
542 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
544 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
549 va_start(ap
, format
);
550 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
553 /* Some complicated logic needed to handle the return value of
557 written
= MIN2(sctx
->left
, written
);
558 sctx
->ptr
+= written
;
559 sctx
->left
-= written
;
566 const struct tgsi_token
*tokens
,
571 struct str_dump_ctx ctx
;
573 ctx
.base
.iter
.prolog
= prolog
;
574 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
575 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
576 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
577 ctx
.base
.iter
.epilog
= NULL
;
581 ctx
.base
.printf
= &str_dump_ctx_printf
;
582 ctx
.base
.indentation
= 0;
587 ctx
.left
= (int)size
;
589 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);