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
;
49 void (*printf
)(struct dump_ctx
*ctx
, const char *format
, ...);
53 dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
58 debug_vprintf(format
, ap
);
70 ctx
->printf( ctx
, "%u", e
);
72 ctx
->printf( ctx
, "%s", enums
[e
] );
75 #define EOL() ctx->printf( ctx, "\n" )
76 #define TXT(S) ctx->printf( ctx, "%s", S )
77 #define CHR(C) ctx->printf( ctx, "%c", C )
78 #define UIX(I) ctx->printf( ctx, "0x%x", I )
79 #define UID(I) ctx->printf( ctx, "%u", I )
80 #define INSTID(I) ctx->printf( ctx, "% 3u", I )
81 #define SID(I) ctx->printf( ctx, "%d", I )
82 #define FLT(F) ctx->printf( ctx, "%10.4f", F )
83 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
85 static const char *processor_type_names
[] =
92 static const char *file_names
[TGSI_FILE_COUNT
] =
105 static const char *interpolate_names
[] =
112 static const char *semantic_names
[] =
124 static const char *immediate_type_names
[] =
129 static const char *swizzle_names
[] =
137 static const char *texture_names
[] =
150 static const char *extswizzle_names
[] =
160 static const char *modulate_names
[TGSI_MODULATE_COUNT
] =
173 struct dump_ctx
*ctx
,
178 ENM( file
, file_names
);
190 struct dump_ctx
*ctx
,
197 ENM( file
, file_names
);
199 ENM( ind_file
, file_names
);
203 ENM( ind_swizzle
, swizzle_names
);
214 struct dump_ctx
*ctx
,
217 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
219 if (writemask
& TGSI_WRITEMASK_X
)
221 if (writemask
& TGSI_WRITEMASK_Y
)
223 if (writemask
& TGSI_WRITEMASK_Z
)
225 if (writemask
& TGSI_WRITEMASK_W
)
232 struct tgsi_iterate_context
*iter
,
233 struct tgsi_full_declaration
*decl
)
235 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
237 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
238 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
244 decl
->Declaration
.File
,
245 decl
->DeclarationRange
.First
,
246 decl
->DeclarationRange
.Last
);
249 decl
->Declaration
.UsageMask
);
251 if (decl
->Declaration
.Semantic
) {
253 ENM( decl
->Semantic
.SemanticName
, semantic_names
);
254 if (decl
->Semantic
.SemanticIndex
!= 0 ||
255 decl
->Semantic
.SemanticName
== TGSI_SEMANTIC_GENERIC
) {
257 UID( decl
->Semantic
.SemanticIndex
);
262 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
263 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
266 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
269 if (decl
->Declaration
.Centroid
) {
273 if (decl
->Declaration
.Invariant
) {
274 TXT( ", INVARIANT" );
283 tgsi_dump_declaration(
284 const struct tgsi_full_declaration
*decl
)
288 ctx
.printf
= dump_ctx_printf
;
290 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
295 struct tgsi_iterate_context
*iter
,
296 struct tgsi_full_immediate
*imm
)
298 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
303 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
307 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
308 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
309 switch (imm
->Immediate
.DataType
) {
310 case TGSI_IMM_FLOAT32
:
311 FLT( imm
->u
[i
].Float
);
317 if (i
< imm
->Immediate
.NrTokens
- 2)
329 const struct tgsi_full_immediate
*imm
)
333 ctx
.printf
= dump_ctx_printf
;
335 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
339 indent(struct dump_ctx
*ctx
)
342 for (i
= 0; i
< ctx
->indentation
; i
++)
348 struct tgsi_iterate_context
*iter
,
349 struct tgsi_full_instruction
*inst
)
351 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
352 uint instno
= ctx
->instno
++;
355 boolean first_reg
= TRUE
;
360 /* update indentation */
361 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_ENDIF
||
362 inst
->Instruction
.Opcode
== TGSI_OPCODE_ENDFOR
||
363 inst
->Instruction
.Opcode
== TGSI_OPCODE_ENDLOOP
) {
364 ctx
->indentation
-= indent_spaces
;
368 TXT( tgsi_get_opcode_info( inst
->Instruction
.Opcode
)->mnemonic
);
370 switch (inst
->Instruction
.Saturate
) {
373 case TGSI_SAT_ZERO_ONE
:
376 case TGSI_SAT_MINUS_PLUS_ONE
:
383 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
384 const struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
390 if (dst
->DstRegister
.Indirect
) {
393 dst
->DstRegister
.File
,
394 dst
->DstRegister
.Index
,
395 dst
->DstRegisterInd
.File
,
396 dst
->DstRegisterInd
.Index
,
397 dst
->DstRegisterInd
.SwizzleX
);
402 dst
->DstRegister
.File
,
403 dst
->DstRegister
.Index
,
404 dst
->DstRegister
.Index
);
406 ENM( dst
->DstRegisterExtModulate
.Modulate
, modulate_names
);
407 _dump_writemask( ctx
, dst
->DstRegister
.WriteMask
);
412 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
413 const struct tgsi_full_src_register
*src
= &inst
->FullSrcRegisters
[i
];
419 if (src
->SrcRegisterExtMod
.Negate
)
421 if (src
->SrcRegisterExtMod
.Absolute
)
423 if (src
->SrcRegisterExtMod
.Scale2X
)
425 if (src
->SrcRegisterExtMod
.Bias
)
427 if (src
->SrcRegisterExtMod
.Complement
)
429 if (src
->SrcRegister
.Negate
)
432 if (src
->SrcRegister
.Indirect
) {
435 src
->SrcRegister
.File
,
436 src
->SrcRegister
.Index
,
437 src
->SrcRegisterInd
.File
,
438 src
->SrcRegisterInd
.Index
,
439 src
->SrcRegisterInd
.SwizzleX
);
444 src
->SrcRegister
.File
,
445 src
->SrcRegister
.Index
,
446 src
->SrcRegister
.Index
);
449 if (src
->SrcRegister
.SwizzleX
!= TGSI_SWIZZLE_X
||
450 src
->SrcRegister
.SwizzleY
!= TGSI_SWIZZLE_Y
||
451 src
->SrcRegister
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
452 src
->SrcRegister
.SwizzleW
!= TGSI_SWIZZLE_W
) {
454 ENM( src
->SrcRegister
.SwizzleX
, swizzle_names
);
455 ENM( src
->SrcRegister
.SwizzleY
, swizzle_names
);
456 ENM( src
->SrcRegister
.SwizzleZ
, swizzle_names
);
457 ENM( src
->SrcRegister
.SwizzleW
, swizzle_names
);
459 if (src
->SrcRegisterExtSwz
.ExtSwizzleX
!= TGSI_EXTSWIZZLE_X
||
460 src
->SrcRegisterExtSwz
.ExtSwizzleY
!= TGSI_EXTSWIZZLE_Y
||
461 src
->SrcRegisterExtSwz
.ExtSwizzleZ
!= TGSI_EXTSWIZZLE_Z
||
462 src
->SrcRegisterExtSwz
.ExtSwizzleW
!= TGSI_EXTSWIZZLE_W
) {
464 if (src
->SrcRegisterExtSwz
.NegateX
)
466 ENM( src
->SrcRegisterExtSwz
.ExtSwizzleX
, extswizzle_names
);
467 if (src
->SrcRegisterExtSwz
.NegateY
)
469 ENM( src
->SrcRegisterExtSwz
.ExtSwizzleY
, extswizzle_names
);
470 if (src
->SrcRegisterExtSwz
.NegateZ
)
472 ENM( src
->SrcRegisterExtSwz
.ExtSwizzleZ
, extswizzle_names
);
473 if (src
->SrcRegisterExtSwz
.NegateW
)
475 ENM( src
->SrcRegisterExtSwz
.ExtSwizzleW
, extswizzle_names
);
478 if (src
->SrcRegisterExtMod
.Complement
)
480 if (src
->SrcRegisterExtMod
.Bias
)
482 if (src
->SrcRegisterExtMod
.Scale2X
)
484 if (src
->SrcRegisterExtMod
.Absolute
)
486 if (src
->SrcRegisterExtMod
.Negate
)
492 if (inst
->InstructionExtTexture
.Texture
!= TGSI_TEXTURE_UNKNOWN
) {
494 ENM( inst
->InstructionExtTexture
.Texture
, texture_names
);
497 switch (inst
->Instruction
.Opcode
) {
499 case TGSI_OPCODE_ELSE
:
500 case TGSI_OPCODE_BGNLOOP
:
501 case TGSI_OPCODE_ENDLOOP
:
502 case TGSI_OPCODE_CAL
:
504 UID( inst
->InstructionExtLabel
.Label
);
508 /* update indentation */
509 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
510 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
511 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
512 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
513 ctx
->indentation
+= indent_spaces
;
522 tgsi_dump_instruction(
523 const struct tgsi_full_instruction
*inst
,
529 ctx
.printf
= dump_ctx_printf
;
532 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
537 struct tgsi_iterate_context
*iter
)
539 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
540 ENM( iter
->processor
.Processor
, processor_type_names
);
541 UID( iter
->version
.MajorVersion
);
543 UID( iter
->version
.MinorVersion
);
550 const struct tgsi_token
*tokens
,
555 ctx
.iter
.prolog
= prolog
;
556 ctx
.iter
.iterate_instruction
= iter_instruction
;
557 ctx
.iter
.iterate_declaration
= iter_declaration
;
558 ctx
.iter
.iterate_immediate
= iter_immediate
;
559 ctx
.iter
.epilog
= NULL
;
562 ctx
.printf
= dump_ctx_printf
;
565 tgsi_iterate_shader( tokens
, &ctx
.iter
);
570 struct dump_ctx base
;
577 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
579 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
584 va_start(ap
, format
);
585 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
588 /* Some complicated logic needed to handle the return value of
592 written
= MIN2(sctx
->left
, written
);
593 sctx
->ptr
+= written
;
594 sctx
->left
-= written
;
601 const struct tgsi_token
*tokens
,
606 struct str_dump_ctx ctx
;
608 ctx
.base
.iter
.prolog
= prolog
;
609 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
610 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
611 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
612 ctx
.base
.iter
.epilog
= NULL
;
615 ctx
.base
.printf
= &str_dump_ctx_printf
;
616 ctx
.base
.indentation
= 0;
621 ctx
.left
= (int)size
;
623 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);