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
[] =
126 static const char *immediate_type_names
[] =
131 static const char *swizzle_names
[] =
139 static const char *texture_names
[] =
153 static const char *modulate_names
[TGSI_MODULATE_COUNT
] =
166 struct dump_ctx
*ctx
,
171 ENM( file
, file_names
);
183 struct dump_ctx
*ctx
,
190 ENM( file
, file_names
);
192 ENM( ind_file
, file_names
);
196 ENM( ind_swizzle
, swizzle_names
);
207 struct dump_ctx
*ctx
,
210 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
212 if (writemask
& TGSI_WRITEMASK_X
)
214 if (writemask
& TGSI_WRITEMASK_Y
)
216 if (writemask
& TGSI_WRITEMASK_Z
)
218 if (writemask
& TGSI_WRITEMASK_W
)
225 struct tgsi_iterate_context
*iter
,
226 struct tgsi_full_declaration
*decl
)
228 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
230 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
231 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
237 decl
->Declaration
.File
,
238 decl
->DeclarationRange
.First
,
239 decl
->DeclarationRange
.Last
);
242 decl
->Declaration
.UsageMask
);
244 if (decl
->Declaration
.Semantic
) {
246 ENM( decl
->Semantic
.SemanticName
, semantic_names
);
247 if (decl
->Semantic
.SemanticIndex
!= 0 ||
248 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
) {
250 UID( decl
->Semantic
.SemanticIndex
);
255 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
256 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
259 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
262 if (decl
->Declaration
.Centroid
) {
266 if (decl
->Declaration
.Invariant
) {
267 TXT( ", INVARIANT" );
276 tgsi_dump_declaration(
277 const struct tgsi_full_declaration
*decl
)
281 ctx
.printf
= dump_ctx_printf
;
283 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
288 struct tgsi_iterate_context
*iter
,
289 struct tgsi_full_immediate
*imm
)
291 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
296 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
300 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
301 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
302 switch (imm
->Immediate
.DataType
) {
303 case TGSI_IMM_FLOAT32
:
304 FLT( imm
->u
[i
].Float
);
310 if (i
< imm
->Immediate
.NrTokens
- 2)
322 const struct tgsi_full_immediate
*imm
)
326 ctx
.printf
= dump_ctx_printf
;
328 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
333 struct tgsi_iterate_context
*iter
,
334 struct tgsi_full_instruction
*inst
)
336 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
337 uint instno
= ctx
->instno
++;
338 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
340 boolean first_reg
= TRUE
;
345 ctx
->indent
-= info
->pre_dedent
;
346 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
348 ctx
->indent
+= info
->post_indent
;
350 TXT( info
->mnemonic
);
352 switch (inst
->Instruction
.Saturate
) {
355 case TGSI_SAT_ZERO_ONE
:
358 case TGSI_SAT_MINUS_PLUS_ONE
:
365 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
366 const struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
372 if (dst
->DstRegister
.Indirect
) {
375 dst
->DstRegister
.File
,
376 dst
->DstRegister
.Index
,
377 dst
->DstRegisterInd
.File
,
378 dst
->DstRegisterInd
.Index
,
379 dst
->DstRegisterInd
.SwizzleX
);
384 dst
->DstRegister
.File
,
385 dst
->DstRegister
.Index
,
386 dst
->DstRegister
.Index
);
388 ENM( dst
->DstRegisterExtModulate
.Modulate
, modulate_names
);
389 _dump_writemask( ctx
, dst
->DstRegister
.WriteMask
);
394 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
395 const struct tgsi_full_src_register
*src
= &inst
->FullSrcRegisters
[i
];
401 if (src
->SrcRegisterExtMod
.Negate
)
403 if (src
->SrcRegisterExtMod
.Absolute
)
405 if (src
->SrcRegisterExtMod
.Scale2X
)
407 if (src
->SrcRegisterExtMod
.Bias
)
409 if (src
->SrcRegisterExtMod
.Complement
)
411 if (src
->SrcRegister
.Negate
)
414 if (src
->SrcRegister
.Indirect
) {
417 src
->SrcRegister
.File
,
418 src
->SrcRegister
.Index
,
419 src
->SrcRegisterInd
.File
,
420 src
->SrcRegisterInd
.Index
,
421 src
->SrcRegisterInd
.SwizzleX
);
426 src
->SrcRegister
.File
,
427 src
->SrcRegister
.Index
,
428 src
->SrcRegister
.Index
);
431 if (src
->SrcRegister
.SwizzleX
!= TGSI_SWIZZLE_X
||
432 src
->SrcRegister
.SwizzleY
!= TGSI_SWIZZLE_Y
||
433 src
->SrcRegister
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
434 src
->SrcRegister
.SwizzleW
!= TGSI_SWIZZLE_W
) {
436 ENM( src
->SrcRegister
.SwizzleX
, swizzle_names
);
437 ENM( src
->SrcRegister
.SwizzleY
, swizzle_names
);
438 ENM( src
->SrcRegister
.SwizzleZ
, swizzle_names
);
439 ENM( src
->SrcRegister
.SwizzleW
, swizzle_names
);
442 if (src
->SrcRegisterExtMod
.Complement
)
444 if (src
->SrcRegisterExtMod
.Bias
)
446 if (src
->SrcRegisterExtMod
.Scale2X
)
448 if (src
->SrcRegisterExtMod
.Absolute
)
450 if (src
->SrcRegisterExtMod
.Negate
)
456 if (inst
->InstructionExtTexture
.Texture
!= TGSI_TEXTURE_UNKNOWN
) {
458 ENM( inst
->InstructionExtTexture
.Texture
, texture_names
);
461 switch (inst
->Instruction
.Opcode
) {
463 case TGSI_OPCODE_ELSE
:
464 case TGSI_OPCODE_BGNLOOP
:
465 case TGSI_OPCODE_ENDLOOP
:
466 case TGSI_OPCODE_CAL
:
468 UID( inst
->InstructionExtLabel
.Label
);
472 /* update indentation */
473 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
474 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
475 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
476 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
477 ctx
->indentation
+= indent_spaces
;
486 tgsi_dump_instruction(
487 const struct tgsi_full_instruction
*inst
,
494 ctx
.printf
= dump_ctx_printf
;
497 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
502 struct tgsi_iterate_context
*iter
)
504 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
505 ENM( iter
->processor
.Processor
, processor_type_names
);
506 UID( iter
->version
.MajorVersion
);
508 UID( iter
->version
.MinorVersion
);
515 const struct tgsi_token
*tokens
,
520 ctx
.iter
.prolog
= prolog
;
521 ctx
.iter
.iterate_instruction
= iter_instruction
;
522 ctx
.iter
.iterate_declaration
= iter_declaration
;
523 ctx
.iter
.iterate_immediate
= iter_immediate
;
524 ctx
.iter
.epilog
= NULL
;
528 ctx
.printf
= dump_ctx_printf
;
531 tgsi_iterate_shader( tokens
, &ctx
.iter
);
536 struct dump_ctx base
;
543 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
545 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
550 va_start(ap
, format
);
551 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
554 /* Some complicated logic needed to handle the return value of
558 written
= MIN2(sctx
->left
, written
);
559 sctx
->ptr
+= written
;
560 sctx
->left
-= written
;
567 const struct tgsi_token
*tokens
,
572 struct str_dump_ctx ctx
;
574 ctx
.base
.iter
.prolog
= prolog
;
575 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
576 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
577 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
578 ctx
.base
.iter
.epilog
= NULL
;
582 ctx
.base
.printf
= &str_dump_ctx_printf
;
583 ctx
.base
.indentation
= 0;
588 ctx
.left
= (int)size
;
590 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);