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
] =
107 static const char *interpolate_names
[] =
114 static const char *semantic_names
[] =
127 static const char *immediate_type_names
[] =
132 static const char *swizzle_names
[] =
140 static const char *texture_names
[] =
156 struct dump_ctx
*ctx
,
161 ENM( file
, file_names
);
173 struct dump_ctx
*ctx
,
180 ENM( file
, file_names
);
182 ENM( ind_file
, file_names
);
186 ENM( ind_swizzle
, swizzle_names
);
197 struct dump_ctx
*ctx
,
200 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
202 if (writemask
& TGSI_WRITEMASK_X
)
204 if (writemask
& TGSI_WRITEMASK_Y
)
206 if (writemask
& TGSI_WRITEMASK_Z
)
208 if (writemask
& TGSI_WRITEMASK_W
)
215 struct tgsi_iterate_context
*iter
,
216 struct tgsi_full_declaration
*decl
)
218 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
220 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
221 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
227 decl
->Declaration
.File
,
232 decl
->Declaration
.UsageMask
);
234 if (decl
->Declaration
.Semantic
) {
236 ENM( decl
->Semantic
.Name
, semantic_names
);
237 if (decl
->Semantic
.Index
!= 0 ||
238 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
240 UID( decl
->Semantic
.Index
);
245 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
246 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
249 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
252 if (decl
->Declaration
.Centroid
) {
256 if (decl
->Declaration
.Invariant
) {
257 TXT( ", INVARIANT" );
266 tgsi_dump_declaration(
267 const struct tgsi_full_declaration
*decl
)
271 ctx
.printf
= dump_ctx_printf
;
273 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
278 struct tgsi_iterate_context
*iter
,
279 struct tgsi_full_immediate
*imm
)
281 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
286 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
290 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
291 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
292 switch (imm
->Immediate
.DataType
) {
293 case TGSI_IMM_FLOAT32
:
294 FLT( imm
->u
[i
].Float
);
300 if (i
< imm
->Immediate
.NrTokens
- 2)
312 const struct tgsi_full_immediate
*imm
)
316 ctx
.printf
= dump_ctx_printf
;
318 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
323 struct tgsi_iterate_context
*iter
,
324 struct tgsi_full_instruction
*inst
)
326 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
327 uint instno
= ctx
->instno
++;
328 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
330 boolean first_reg
= TRUE
;
335 ctx
->indent
-= info
->pre_dedent
;
336 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
338 ctx
->indent
+= info
->post_indent
;
340 TXT( info
->mnemonic
);
342 switch (inst
->Instruction
.Saturate
) {
345 case TGSI_SAT_ZERO_ONE
:
348 case TGSI_SAT_MINUS_PLUS_ONE
:
355 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
356 const struct tgsi_full_dst_register
*dst
= &inst
->Dst
[i
];
362 if (dst
->Register
.Indirect
) {
369 dst
->Indirect
.SwizzleX
);
376 dst
->Register
.Index
);
378 _dump_writemask( ctx
, dst
->Register
.WriteMask
);
383 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
384 const struct tgsi_full_src_register
*src
= &inst
->Src
[i
];
390 if (src
->Register
.Negate
)
392 if (src
->Register
.Absolute
)
395 if (src
->Register
.Indirect
) {
402 src
->Indirect
.SwizzleX
);
409 src
->Register
.Index
);
412 if (src
->Register
.SwizzleX
!= TGSI_SWIZZLE_X
||
413 src
->Register
.SwizzleY
!= TGSI_SWIZZLE_Y
||
414 src
->Register
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
415 src
->Register
.SwizzleW
!= TGSI_SWIZZLE_W
) {
417 ENM( src
->Register
.SwizzleX
, swizzle_names
);
418 ENM( src
->Register
.SwizzleY
, swizzle_names
);
419 ENM( src
->Register
.SwizzleZ
, swizzle_names
);
420 ENM( src
->Register
.SwizzleW
, swizzle_names
);
423 if (src
->Register
.Absolute
)
425 if (src
->Register
.Negate
)
431 if (inst
->Instruction
.Texture
) {
433 ENM( inst
->Texture
.Texture
, texture_names
);
436 switch (inst
->Instruction
.Opcode
) {
438 case TGSI_OPCODE_ELSE
:
439 case TGSI_OPCODE_BGNLOOP
:
440 case TGSI_OPCODE_ENDLOOP
:
441 case TGSI_OPCODE_CAL
:
443 UID( inst
->Label
.Label
);
447 /* update indentation */
448 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
449 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
450 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
451 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
452 ctx
->indentation
+= indent_spaces
;
461 tgsi_dump_instruction(
462 const struct tgsi_full_instruction
*inst
,
469 ctx
.printf
= dump_ctx_printf
;
472 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
477 struct tgsi_iterate_context
*iter
)
479 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
480 ENM( iter
->processor
.Processor
, processor_type_names
);
487 const struct tgsi_token
*tokens
,
492 ctx
.iter
.prolog
= prolog
;
493 ctx
.iter
.iterate_instruction
= iter_instruction
;
494 ctx
.iter
.iterate_declaration
= iter_declaration
;
495 ctx
.iter
.iterate_immediate
= iter_immediate
;
496 ctx
.iter
.epilog
= NULL
;
500 ctx
.printf
= dump_ctx_printf
;
503 tgsi_iterate_shader( tokens
, &ctx
.iter
);
508 struct dump_ctx base
;
515 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
517 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
522 va_start(ap
, format
);
523 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
526 /* Some complicated logic needed to handle the return value of
530 written
= MIN2(sctx
->left
, written
);
531 sctx
->ptr
+= written
;
532 sctx
->left
-= written
;
539 const struct tgsi_token
*tokens
,
544 struct str_dump_ctx ctx
;
546 ctx
.base
.iter
.prolog
= prolog
;
547 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
548 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
549 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
550 ctx
.base
.iter
.epilog
= NULL
;
554 ctx
.base
.printf
= &str_dump_ctx_printf
;
555 ctx
.base
.indentation
= 0;
560 ctx
.left
= (int)size
;
562 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);