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
[] =
155 struct dump_ctx
*ctx
,
160 ENM( file
, file_names
);
172 struct dump_ctx
*ctx
,
179 ENM( file
, file_names
);
181 ENM( ind_file
, file_names
);
185 ENM( ind_swizzle
, swizzle_names
);
196 struct dump_ctx
*ctx
,
199 if (writemask
!= TGSI_WRITEMASK_XYZW
) {
201 if (writemask
& TGSI_WRITEMASK_X
)
203 if (writemask
& TGSI_WRITEMASK_Y
)
205 if (writemask
& TGSI_WRITEMASK_Z
)
207 if (writemask
& TGSI_WRITEMASK_W
)
214 struct tgsi_iterate_context
*iter
,
215 struct tgsi_full_declaration
*decl
)
217 struct dump_ctx
*ctx
= (struct dump_ctx
*)iter
;
219 assert(Elements(semantic_names
) == TGSI_SEMANTIC_COUNT
);
220 assert(Elements(interpolate_names
) == TGSI_INTERPOLATE_COUNT
);
226 decl
->Declaration
.File
,
227 decl
->DeclarationRange
.First
,
228 decl
->DeclarationRange
.Last
);
231 decl
->Declaration
.UsageMask
);
233 if (decl
->Declaration
.Semantic
) {
235 ENM( decl
->Semantic
.Name
, semantic_names
);
236 if (decl
->Semantic
.Index
!= 0 ||
237 decl
->Semantic
.Name
== TGSI_SEMANTIC_GENERIC
) {
239 UID( decl
->Semantic
.Index
);
244 if (iter
->processor
.Processor
== TGSI_PROCESSOR_FRAGMENT
&&
245 decl
->Declaration
.File
== TGSI_FILE_INPUT
)
248 ENM( decl
->Declaration
.Interpolate
, interpolate_names
);
251 if (decl
->Declaration
.Centroid
) {
255 if (decl
->Declaration
.Invariant
) {
256 TXT( ", INVARIANT" );
265 tgsi_dump_declaration(
266 const struct tgsi_full_declaration
*decl
)
270 ctx
.printf
= dump_ctx_printf
;
272 iter_declaration( &ctx
.iter
, (struct tgsi_full_declaration
*)decl
);
277 struct tgsi_iterate_context
*iter
,
278 struct tgsi_full_immediate
*imm
)
280 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
285 ENM( imm
->Immediate
.DataType
, immediate_type_names
);
289 assert( imm
->Immediate
.NrTokens
<= 4 + 1 );
290 for (i
= 0; i
< imm
->Immediate
.NrTokens
- 1; i
++) {
291 switch (imm
->Immediate
.DataType
) {
292 case TGSI_IMM_FLOAT32
:
293 FLT( imm
->u
[i
].Float
);
299 if (i
< imm
->Immediate
.NrTokens
- 2)
311 const struct tgsi_full_immediate
*imm
)
315 ctx
.printf
= dump_ctx_printf
;
317 iter_immediate( &ctx
.iter
, (struct tgsi_full_immediate
*)imm
);
322 struct tgsi_iterate_context
*iter
,
323 struct tgsi_full_instruction
*inst
)
325 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
326 uint instno
= ctx
->instno
++;
327 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( inst
->Instruction
.Opcode
);
329 boolean first_reg
= TRUE
;
334 ctx
->indent
-= info
->pre_dedent
;
335 for(i
= 0; (int)i
< ctx
->indent
; ++i
)
337 ctx
->indent
+= info
->post_indent
;
339 TXT( info
->mnemonic
);
341 switch (inst
->Instruction
.Saturate
) {
344 case TGSI_SAT_ZERO_ONE
:
347 case TGSI_SAT_MINUS_PLUS_ONE
:
354 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; i
++) {
355 const struct tgsi_full_dst_register
*dst
= &inst
->FullDstRegisters
[i
];
361 if (dst
->DstRegister
.Indirect
) {
364 dst
->DstRegister
.File
,
365 dst
->DstRegister
.Index
,
366 dst
->DstRegisterInd
.File
,
367 dst
->DstRegisterInd
.Index
,
368 dst
->DstRegisterInd
.SwizzleX
);
373 dst
->DstRegister
.File
,
374 dst
->DstRegister
.Index
,
375 dst
->DstRegister
.Index
);
377 _dump_writemask( ctx
, dst
->DstRegister
.WriteMask
);
382 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
383 const struct tgsi_full_src_register
*src
= &inst
->FullSrcRegisters
[i
];
389 if (src
->SrcRegister
.Negate
)
391 if (src
->SrcRegister
.Absolute
)
394 if (src
->SrcRegister
.Indirect
) {
397 src
->SrcRegister
.File
,
398 src
->SrcRegister
.Index
,
399 src
->SrcRegisterInd
.File
,
400 src
->SrcRegisterInd
.Index
,
401 src
->SrcRegisterInd
.SwizzleX
);
406 src
->SrcRegister
.File
,
407 src
->SrcRegister
.Index
,
408 src
->SrcRegister
.Index
);
411 if (src
->SrcRegister
.SwizzleX
!= TGSI_SWIZZLE_X
||
412 src
->SrcRegister
.SwizzleY
!= TGSI_SWIZZLE_Y
||
413 src
->SrcRegister
.SwizzleZ
!= TGSI_SWIZZLE_Z
||
414 src
->SrcRegister
.SwizzleW
!= TGSI_SWIZZLE_W
) {
416 ENM( src
->SrcRegister
.SwizzleX
, swizzle_names
);
417 ENM( src
->SrcRegister
.SwizzleY
, swizzle_names
);
418 ENM( src
->SrcRegister
.SwizzleZ
, swizzle_names
);
419 ENM( src
->SrcRegister
.SwizzleW
, swizzle_names
);
422 if (src
->SrcRegister
.Absolute
)
424 if (src
->SrcRegister
.Negate
)
430 if (inst
->Instruction
.Texture
) {
432 ENM( inst
->InstructionTexture
.Texture
, texture_names
);
435 switch (inst
->Instruction
.Opcode
) {
437 case TGSI_OPCODE_ELSE
:
438 case TGSI_OPCODE_BGNLOOP
:
439 case TGSI_OPCODE_ENDLOOP
:
440 case TGSI_OPCODE_CAL
:
442 UID( inst
->InstructionLabel
.Label
);
446 /* update indentation */
447 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_IF
||
448 inst
->Instruction
.Opcode
== TGSI_OPCODE_ELSE
||
449 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNFOR
||
450 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNLOOP
) {
451 ctx
->indentation
+= indent_spaces
;
460 tgsi_dump_instruction(
461 const struct tgsi_full_instruction
*inst
,
468 ctx
.printf
= dump_ctx_printf
;
471 iter_instruction( &ctx
.iter
, (struct tgsi_full_instruction
*)inst
);
476 struct tgsi_iterate_context
*iter
)
478 struct dump_ctx
*ctx
= (struct dump_ctx
*) iter
;
479 ENM( iter
->processor
.Processor
, processor_type_names
);
480 UID( iter
->version
.Major
);
482 UID( iter
->version
.Minor
);
489 const struct tgsi_token
*tokens
,
494 ctx
.iter
.prolog
= prolog
;
495 ctx
.iter
.iterate_instruction
= iter_instruction
;
496 ctx
.iter
.iterate_declaration
= iter_declaration
;
497 ctx
.iter
.iterate_immediate
= iter_immediate
;
498 ctx
.iter
.epilog
= NULL
;
502 ctx
.printf
= dump_ctx_printf
;
505 tgsi_iterate_shader( tokens
, &ctx
.iter
);
510 struct dump_ctx base
;
517 str_dump_ctx_printf(struct dump_ctx
*ctx
, const char *format
, ...)
519 struct str_dump_ctx
*sctx
= (struct str_dump_ctx
*)ctx
;
524 va_start(ap
, format
);
525 written
= util_vsnprintf(sctx
->ptr
, sctx
->left
, format
, ap
);
528 /* Some complicated logic needed to handle the return value of
532 written
= MIN2(sctx
->left
, written
);
533 sctx
->ptr
+= written
;
534 sctx
->left
-= written
;
541 const struct tgsi_token
*tokens
,
546 struct str_dump_ctx ctx
;
548 ctx
.base
.iter
.prolog
= prolog
;
549 ctx
.base
.iter
.iterate_instruction
= iter_instruction
;
550 ctx
.base
.iter
.iterate_declaration
= iter_declaration
;
551 ctx
.base
.iter
.iterate_immediate
= iter_immediate
;
552 ctx
.base
.iter
.epilog
= NULL
;
556 ctx
.base
.printf
= &str_dump_ctx_printf
;
557 ctx
.base
.indentation
= 0;
562 ctx
.left
= (int)size
;
564 tgsi_iterate_shader( tokens
, &ctx
.base
.iter
);