tgsi_dump.c revision 9b21b3c52a8a7d58d08151d1a6bf25c472dec213
1/**************************************************************************
2 *
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
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"
35
36
37/** Number of spaces to indent for IF/LOOP/etc */
38static const int indent_spaces = 3;
39
40
41struct dump_ctx
42{
43   struct tgsi_iterate_context iter;
44
45   uint instno;
46   int indent;
47
48   uint indentation;
49
50   void (*printf)(struct dump_ctx *ctx, const char *format, ...);
51};
52
53static void
54dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
55{
56   va_list ap;
57   (void)ctx;
58   va_start(ap, format);
59   debug_vprintf(format, ap);
60   va_end(ap);
61}
62
63static void
64dump_enum(
65   struct dump_ctx *ctx,
66   uint e,
67   const char **enums,
68   uint enum_count )
69{
70   if (e >= enum_count)
71      ctx->printf( ctx, "%u", e );
72   else
73      ctx->printf( ctx, "%s", enums[e] );
74}
75
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 ) )
85
86static const char *processor_type_names[] =
87{
88   "FRAG",
89   "VERT",
90   "GEOM"
91};
92
93static const char *file_names[TGSI_FILE_COUNT] =
94{
95   "NULL",
96   "CONST",
97   "IN",
98   "OUT",
99   "TEMP",
100   "SAMP",
101   "ADDR",
102   "IMM",
103   "LOOP",
104   "PRED",
105   "SV"
106};
107
108static const char *interpolate_names[] =
109{
110   "CONSTANT",
111   "LINEAR",
112   "PERSPECTIVE"
113};
114
115static const char *semantic_names[] =
116{
117   "POSITION",
118   "COLOR",
119   "BCOLOR",
120   "FOG",
121   "PSIZE",
122   "GENERIC",
123   "NORMAL",
124   "FACE",
125   "EDGEFLAG",
126   "PRIM_ID",
127   "INSTANCEID"
128};
129
130static const char *immediate_type_names[] =
131{
132   "FLT32",
133   "UINT32",
134   "INT32"
135};
136
137static const char *swizzle_names[] =
138{
139   "x",
140   "y",
141   "z",
142   "w"
143};
144
145static const char *texture_names[] =
146{
147   "UNKNOWN",
148   "1D",
149   "2D",
150   "3D",
151   "CUBE",
152   "RECT",
153   "SHADOW1D",
154   "SHADOW2D",
155   "SHADOWRECT"
156};
157
158static const char *property_names[] =
159{
160   "GS_INPUT_PRIMITIVE",
161   "GS_OUTPUT_PRIMITIVE",
162   "GS_MAX_OUTPUT_VERTICES"
163};
164
165static const char *primitive_names[] =
166{
167   "POINTS",
168   "LINES",
169   "LINE_LOOP",
170   "LINE_STRIP",
171   "TRIANGLES",
172   "TRIANGLE_STRIP",
173   "TRIANGLE_FAN",
174   "QUADS",
175   "QUAD_STRIP",
176   "POLYGON"
177};
178
179
180static void
181_dump_register_decl(
182   struct dump_ctx *ctx,
183   uint file,
184   int first,
185   int last )
186{
187   ENM( file, file_names );
188
189   /* all geometry shader inputs are two dimensional */
190   if (file == TGSI_FILE_INPUT &&
191       ctx->iter.processor.Processor == TGSI_PROCESSOR_GEOMETRY)
192      TXT("[]");
193
194   CHR( '[' );
195   SID( first );
196   if (first != last) {
197      TXT( ".." );
198      SID( last );
199   }
200   CHR( ']' );
201}
202
203static void
204_dump_register_dst(
205   struct dump_ctx *ctx,
206   uint file,
207   int index)
208{
209   ENM( file, file_names );
210
211   CHR( '[' );
212   SID( index );
213   CHR( ']' );
214}
215
216
217static void
218_dump_register_src(
219   struct dump_ctx *ctx,
220   const struct tgsi_full_src_register *src )
221{
222   if (src->Register.Indirect) {
223      ENM( src->Register.File, file_names );
224      CHR( '[' );
225      ENM( src->Indirect.File, file_names );
226      CHR( '[' );
227      SID( src->Indirect.Index );
228      TXT( "]." );
229      ENM( src->Indirect.SwizzleX, swizzle_names );
230      if (src->Register.Index != 0) {
231         if (src->Register.Index > 0)
232            CHR( '+' );
233         SID( src->Register.Index );
234      }
235      CHR( ']' );
236   } else {
237      ENM( src->Register.File, file_names );
238      CHR( '[' );
239      SID( src->Register.Index );
240      CHR( ']' );
241   }
242   if (src->Register.Dimension) {
243      CHR( '[' );
244      SID( src->Dimension.Index );
245      CHR( ']' );
246   }
247}
248
249static void
250_dump_register_ind(
251   struct dump_ctx *ctx,
252   uint file,
253   int index,
254   uint ind_file,
255   int ind_index,
256   uint ind_swizzle )
257{
258   ENM( file, file_names );
259   CHR( '[' );
260   ENM( ind_file, file_names );
261   CHR( '[' );
262   SID( ind_index );
263   TXT( "]." );
264   ENM( ind_swizzle, swizzle_names );
265   if (index != 0) {
266      if (index > 0)
267         CHR( '+' );
268      SID( index );
269   }
270   CHR( ']' );
271}
272
273static void
274_dump_writemask(
275   struct dump_ctx *ctx,
276   uint writemask )
277{
278   if (writemask != TGSI_WRITEMASK_XYZW) {
279      CHR( '.' );
280      if (writemask & TGSI_WRITEMASK_X)
281         CHR( 'x' );
282      if (writemask & TGSI_WRITEMASK_Y)
283         CHR( 'y' );
284      if (writemask & TGSI_WRITEMASK_Z)
285         CHR( 'z' );
286      if (writemask & TGSI_WRITEMASK_W)
287         CHR( 'w' );
288   }
289}
290
291static boolean
292iter_declaration(
293   struct tgsi_iterate_context *iter,
294   struct tgsi_full_declaration *decl )
295{
296   struct dump_ctx *ctx = (struct dump_ctx *)iter;
297
298   assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT);
299   assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT);
300
301   TXT( "DCL " );
302
303   _dump_register_decl(
304      ctx,
305      decl->Declaration.File,
306      decl->Range.First,
307      decl->Range.Last );
308   _dump_writemask(
309      ctx,
310      decl->Declaration.UsageMask );
311
312   if (decl->Declaration.Semantic) {
313      TXT( ", " );
314      ENM( decl->Semantic.Name, semantic_names );
315      if (decl->Semantic.Index != 0 ||
316          decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) {
317         CHR( '[' );
318         UID( decl->Semantic.Index );
319         CHR( ']' );
320      }
321   }
322
323   if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT &&
324       decl->Declaration.File == TGSI_FILE_INPUT)
325   {
326      TXT( ", " );
327      ENM( decl->Declaration.Interpolate, interpolate_names );
328   }
329
330   if (decl->Declaration.Centroid) {
331      TXT( ", CENTROID" );
332   }
333
334   if (decl->Declaration.Invariant) {
335      TXT( ", INVARIANT" );
336   }
337
338   EOL();
339
340   return TRUE;
341}
342
343void
344tgsi_dump_declaration(
345   const struct tgsi_full_declaration *decl )
346{
347   struct dump_ctx ctx;
348
349   ctx.printf = dump_ctx_printf;
350
351   iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl );
352}
353
354static boolean
355iter_property(
356   struct tgsi_iterate_context *iter,
357   struct tgsi_full_property *prop )
358{
359   int i;
360   struct dump_ctx *ctx = (struct dump_ctx *)iter;
361
362   assert(Elements(property_names) == TGSI_PROPERTY_COUNT);
363
364   TXT( "PROPERTY " );
365   ENM(prop->Property.PropertyName, property_names);
366
367   if (prop->Property.NrTokens > 1)
368      TXT(" ");
369
370   for (i = 0; i < prop->Property.NrTokens - 1; ++i) {
371      switch (prop->Property.PropertyName) {
372      case TGSI_PROPERTY_GS_INPUT_PRIM:
373      case TGSI_PROPERTY_GS_OUTPUT_PRIM:
374         ENM(prop->u[i].Data, primitive_names);
375         break;
376      default:
377         SID( prop->u[i].Data );
378         break;
379      }
380      if (i < prop->Property.NrTokens - 2)
381         TXT( ", " );
382   }
383   EOL();
384
385   return TRUE;
386}
387
388void tgsi_dump_property(
389   const struct tgsi_full_property *prop )
390{
391   struct dump_ctx ctx;
392
393   ctx.printf = dump_ctx_printf;
394
395   iter_property( &ctx.iter, (struct tgsi_full_property *)prop );
396}
397
398static boolean
399iter_immediate(
400   struct tgsi_iterate_context *iter,
401   struct tgsi_full_immediate *imm )
402{
403   struct dump_ctx *ctx = (struct dump_ctx *) iter;
404
405   uint i;
406
407   TXT( "IMM " );
408   ENM( imm->Immediate.DataType, immediate_type_names );
409
410   TXT( " { " );
411
412   assert( imm->Immediate.NrTokens <= 4 + 1 );
413   for (i = 0; i < imm->Immediate.NrTokens - 1; i++) {
414      switch (imm->Immediate.DataType) {
415      case TGSI_IMM_FLOAT32:
416         FLT( imm->u[i].Float );
417         break;
418      case TGSI_IMM_UINT32:
419         UID(imm->u[i].Uint);
420         break;
421      case TGSI_IMM_INT32:
422         SID(imm->u[i].Int);
423         break;
424      default:
425         assert( 0 );
426      }
427
428      if (i < imm->Immediate.NrTokens - 2)
429         TXT( ", " );
430   }
431   TXT( " }" );
432
433   EOL();
434
435   return TRUE;
436}
437
438void
439tgsi_dump_immediate(
440   const struct tgsi_full_immediate *imm )
441{
442   struct dump_ctx ctx;
443
444   ctx.printf = dump_ctx_printf;
445
446   iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm );
447}
448
449static boolean
450iter_instruction(
451   struct tgsi_iterate_context *iter,
452   struct tgsi_full_instruction *inst )
453{
454   struct dump_ctx *ctx = (struct dump_ctx *) iter;
455   uint instno = ctx->instno++;
456   const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode );
457   uint i;
458   boolean first_reg = TRUE;
459
460   INSTID( instno );
461   TXT( ": " );
462
463   ctx->indent -= info->pre_dedent;
464   for(i = 0; (int)i < ctx->indent; ++i)
465      TXT( "  " );
466   ctx->indent += info->post_indent;
467
468   TXT( info->mnemonic );
469
470   switch (inst->Instruction.Saturate) {
471   case TGSI_SAT_NONE:
472      break;
473   case TGSI_SAT_ZERO_ONE:
474      TXT( "_SAT" );
475      break;
476   case TGSI_SAT_MINUS_PLUS_ONE:
477      TXT( "_SATNV" );
478      break;
479   default:
480      assert( 0 );
481   }
482
483   for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
484      const struct tgsi_full_dst_register *dst = &inst->Dst[i];
485
486      if (!first_reg)
487         CHR( ',' );
488      CHR( ' ' );
489
490      if (dst->Register.Indirect) {
491         _dump_register_ind(
492            ctx,
493            dst->Register.File,
494            dst->Register.Index,
495            dst->Indirect.File,
496            dst->Indirect.Index,
497            dst->Indirect.SwizzleX );
498      }
499      else {
500         _dump_register_dst(
501            ctx,
502            dst->Register.File,
503            dst->Register.Index );
504      }
505      _dump_writemask( ctx, dst->Register.WriteMask );
506
507      first_reg = FALSE;
508   }
509
510   for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
511      const struct tgsi_full_src_register *src = &inst->Src[i];
512
513      if (!first_reg)
514         CHR( ',' );
515      CHR( ' ' );
516
517      if (src->Register.Negate)
518         CHR( '-' );
519      if (src->Register.Absolute)
520         CHR( '|' );
521
522      _dump_register_src(ctx, src);
523
524      if (src->Register.SwizzleX != TGSI_SWIZZLE_X ||
525          src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
526          src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
527          src->Register.SwizzleW != TGSI_SWIZZLE_W) {
528         CHR( '.' );
529         ENM( src->Register.SwizzleX, swizzle_names );
530         ENM( src->Register.SwizzleY, swizzle_names );
531         ENM( src->Register.SwizzleZ, swizzle_names );
532         ENM( src->Register.SwizzleW, swizzle_names );
533      }
534
535      if (src->Register.Absolute)
536         CHR( '|' );
537
538      first_reg = FALSE;
539   }
540
541   if (inst->Instruction.Texture) {
542      TXT( ", " );
543      ENM( inst->Texture.Texture, texture_names );
544   }
545
546   switch (inst->Instruction.Opcode) {
547   case TGSI_OPCODE_IF:
548   case TGSI_OPCODE_ELSE:
549   case TGSI_OPCODE_BGNLOOP:
550   case TGSI_OPCODE_ENDLOOP:
551   case TGSI_OPCODE_CAL:
552      TXT( " :" );
553      UID( inst->Label.Label );
554      break;
555   }
556
557   /* update indentation */
558   if (inst->Instruction.Opcode == TGSI_OPCODE_IF ||
559       inst->Instruction.Opcode == TGSI_OPCODE_ELSE ||
560       inst->Instruction.Opcode == TGSI_OPCODE_BGNFOR ||
561       inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) {
562      ctx->indentation += indent_spaces;
563   }
564
565   EOL();
566
567   return TRUE;
568}
569
570void
571tgsi_dump_instruction(
572   const struct tgsi_full_instruction *inst,
573   uint instno )
574{
575   struct dump_ctx ctx;
576
577   ctx.instno = instno;
578   ctx.indent = 0;
579   ctx.printf = dump_ctx_printf;
580   ctx.indentation = 0;
581
582   iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst );
583}
584
585static boolean
586prolog(
587   struct tgsi_iterate_context *iter )
588{
589   struct dump_ctx *ctx = (struct dump_ctx *) iter;
590   ENM( iter->processor.Processor, processor_type_names );
591   EOL();
592   return TRUE;
593}
594
595void
596tgsi_dump(
597   const struct tgsi_token *tokens,
598   uint flags )
599{
600   struct dump_ctx ctx;
601
602   ctx.iter.prolog = prolog;
603   ctx.iter.iterate_instruction = iter_instruction;
604   ctx.iter.iterate_declaration = iter_declaration;
605   ctx.iter.iterate_immediate = iter_immediate;
606   ctx.iter.iterate_property = iter_property;
607   ctx.iter.epilog = NULL;
608
609   ctx.instno = 0;
610   ctx.indent = 0;
611   ctx.printf = dump_ctx_printf;
612   ctx.indentation = 0;
613
614   tgsi_iterate_shader( tokens, &ctx.iter );
615}
616
617struct str_dump_ctx
618{
619   struct dump_ctx base;
620   char *str;
621   char *ptr;
622   int left;
623};
624
625static void
626str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
627{
628   struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx;
629
630   if(sctx->left > 1) {
631      int written;
632      va_list ap;
633      va_start(ap, format);
634      written = util_vsnprintf(sctx->ptr, sctx->left, format, ap);
635      va_end(ap);
636
637      /* Some complicated logic needed to handle the return value of
638       * vsnprintf:
639       */
640      if (written > 0) {
641         written = MIN2(sctx->left, written);
642         sctx->ptr += written;
643         sctx->left -= written;
644      }
645   }
646}
647
648void
649tgsi_dump_str(
650   const struct tgsi_token *tokens,
651   uint flags,
652   char *str,
653   size_t size)
654{
655   struct str_dump_ctx ctx;
656
657   ctx.base.iter.prolog = prolog;
658   ctx.base.iter.iterate_instruction = iter_instruction;
659   ctx.base.iter.iterate_declaration = iter_declaration;
660   ctx.base.iter.iterate_immediate = iter_immediate;
661   ctx.base.iter.iterate_property = iter_property;
662   ctx.base.iter.epilog = NULL;
663
664   ctx.base.instno = 0;
665   ctx.base.indent = 0;
666   ctx.base.printf = &str_dump_ctx_printf;
667   ctx.base.indentation = 0;
668
669   ctx.str = str;
670   ctx.str[0] = 0;
671   ctx.ptr = str;
672   ctx.left = (int)size;
673
674   tgsi_iterate_shader( tokens, &ctx.base.iter );
675}
676