tgsi_dump.c revision 1279923d72942ee201fcc6ad40d552143f651f03
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#include "tgsi_strings.h"
36
37
38/** Number of spaces to indent for IF/LOOP/etc */
39static const int indent_spaces = 3;
40
41
42struct dump_ctx
43{
44   struct tgsi_iterate_context iter;
45
46   uint instno;
47   int indent;
48
49   uint indentation;
50
51   void (*dump_printf)(struct dump_ctx *ctx, const char *format, ...);
52};
53
54static void
55dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
56{
57   va_list ap;
58   (void)ctx;
59   va_start(ap, format);
60   _debug_vprintf(format, ap);
61   va_end(ap);
62}
63
64static void
65dump_enum(
66   struct dump_ctx *ctx,
67   uint e,
68   const char **enums,
69   uint enum_count )
70{
71   if (e >= enum_count)
72      ctx->dump_printf( ctx, "%u", e );
73   else
74      ctx->dump_printf( ctx, "%s", enums[e] );
75}
76
77#define EOL()           ctx->dump_printf( ctx, "\n" )
78#define TXT(S)          ctx->dump_printf( ctx, "%s", S )
79#define CHR(C)          ctx->dump_printf( ctx, "%c", C )
80#define UIX(I)          ctx->dump_printf( ctx, "0x%x", I )
81#define UID(I)          ctx->dump_printf( ctx, "%u", I )
82#define INSTID(I)       ctx->dump_printf( ctx, "% 3u", I )
83#define SID(I)          ctx->dump_printf( ctx, "%d", I )
84#define FLT(F)          ctx->dump_printf( ctx, "%10.4f", F )
85#define ENM(E,ENUMS)    dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
86
87const char *
88tgsi_swizzle_names[4] =
89{
90   "x",
91   "y",
92   "z",
93   "w"
94};
95
96static void
97_dump_register_src(
98   struct dump_ctx *ctx,
99   const struct tgsi_full_src_register *src )
100{
101   ENM(src->Register.File, tgsi_file_names);
102   if (src->Register.Dimension) {
103      if (src->Dimension.Indirect) {
104         CHR( '[' );
105         ENM( src->DimIndirect.File, tgsi_file_names );
106         CHR( '[' );
107         SID( src->DimIndirect.Index );
108         TXT( "]." );
109         ENM( src->DimIndirect.SwizzleX, tgsi_swizzle_names );
110         if (src->Dimension.Index != 0) {
111            if (src->Dimension.Index > 0)
112               CHR( '+' );
113            SID( src->Dimension.Index );
114         }
115         CHR( ']' );
116      } else {
117         CHR('[');
118         SID(src->Dimension.Index);
119         CHR(']');
120      }
121   }
122   if (src->Register.Indirect) {
123      CHR( '[' );
124      ENM( src->Indirect.File, tgsi_file_names );
125      CHR( '[' );
126      SID( src->Indirect.Index );
127      TXT( "]." );
128      ENM( src->Indirect.SwizzleX, tgsi_swizzle_names );
129      if (src->Register.Index != 0) {
130         if (src->Register.Index > 0)
131            CHR( '+' );
132         SID( src->Register.Index );
133      }
134      CHR( ']' );
135   } else {
136      CHR( '[' );
137      SID( src->Register.Index );
138      CHR( ']' );
139   }
140}
141
142
143static void
144_dump_register_dst(
145   struct dump_ctx *ctx,
146   const struct tgsi_full_dst_register *dst )
147{
148   ENM(dst->Register.File, tgsi_file_names);
149   if (dst->Register.Dimension) {
150      if (dst->Dimension.Indirect) {
151         CHR( '[' );
152         ENM( dst->DimIndirect.File, tgsi_file_names );
153         CHR( '[' );
154         SID( dst->DimIndirect.Index );
155         TXT( "]." );
156         ENM( dst->DimIndirect.SwizzleX, tgsi_swizzle_names );
157         if (dst->Dimension.Index != 0) {
158            if (dst->Dimension.Index > 0)
159               CHR( '+' );
160            SID( dst->Dimension.Index );
161         }
162         CHR( ']' );
163      } else {
164         CHR('[');
165         SID(dst->Dimension.Index);
166         CHR(']');
167      }
168   }
169   if (dst->Register.Indirect) {
170      CHR( '[' );
171      ENM( dst->Indirect.File, tgsi_file_names );
172      CHR( '[' );
173      SID( dst->Indirect.Index );
174      TXT( "]." );
175      ENM( dst->Indirect.SwizzleX, tgsi_swizzle_names );
176      if (dst->Register.Index != 0) {
177         if (dst->Register.Index > 0)
178            CHR( '+' );
179         SID( dst->Register.Index );
180      }
181      CHR( ']' );
182   } else {
183      CHR( '[' );
184      SID( dst->Register.Index );
185      CHR( ']' );
186   }
187}
188static void
189_dump_writemask(
190   struct dump_ctx *ctx,
191   uint writemask )
192{
193   if (writemask != TGSI_WRITEMASK_XYZW) {
194      CHR( '.' );
195      if (writemask & TGSI_WRITEMASK_X)
196         CHR( 'x' );
197      if (writemask & TGSI_WRITEMASK_Y)
198         CHR( 'y' );
199      if (writemask & TGSI_WRITEMASK_Z)
200         CHR( 'z' );
201      if (writemask & TGSI_WRITEMASK_W)
202         CHR( 'w' );
203   }
204}
205
206static void
207dump_imm_data(struct tgsi_iterate_context *iter,
208              union tgsi_immediate_data *data,
209              unsigned num_tokens,
210              unsigned data_type)
211{
212   struct dump_ctx *ctx = (struct dump_ctx *)iter;
213   unsigned i ;
214
215   TXT( " {" );
216
217   assert( num_tokens <= 4 );
218   for (i = 0; i < num_tokens; i++) {
219      switch (data_type) {
220      case TGSI_IMM_FLOAT32:
221         FLT( data[i].Float );
222         break;
223      case TGSI_IMM_UINT32:
224         UID(data[i].Uint);
225         break;
226      case TGSI_IMM_INT32:
227         SID(data[i].Int);
228         break;
229      default:
230         assert( 0 );
231      }
232
233      if (i < num_tokens - 1)
234         TXT( ", " );
235   }
236   TXT( "}" );
237}
238
239static boolean
240iter_declaration(
241   struct tgsi_iterate_context *iter,
242   struct tgsi_full_declaration *decl )
243{
244   struct dump_ctx *ctx = (struct dump_ctx *)iter;
245
246   TXT( "DCL " );
247
248   ENM(decl->Declaration.File, tgsi_file_names);
249
250   /* all geometry shader inputs are two dimensional */
251   if (decl->Declaration.File == TGSI_FILE_INPUT &&
252       iter->processor.Processor == TGSI_PROCESSOR_GEOMETRY) {
253      TXT("[]");
254   }
255
256   if (decl->Declaration.Dimension) {
257      CHR('[');
258      SID(decl->Dim.Index2D);
259      CHR(']');
260   }
261
262   CHR('[');
263   SID(decl->Range.First);
264   if (decl->Range.First != decl->Range.Last) {
265      TXT("..");
266      SID(decl->Range.Last);
267   }
268   CHR(']');
269
270   _dump_writemask(
271      ctx,
272      decl->Declaration.UsageMask );
273
274   if (decl->Declaration.Semantic) {
275      TXT( ", " );
276      ENM( decl->Semantic.Name, tgsi_semantic_names );
277      if (decl->Semantic.Index != 0 ||
278          decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) {
279         CHR( '[' );
280         UID( decl->Semantic.Index );
281         CHR( ']' );
282      }
283   }
284
285   if (decl->Declaration.File == TGSI_FILE_RESOURCE) {
286      TXT(", ");
287      ENM(decl->Resource.Resource, tgsi_texture_names);
288   }
289
290   if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {
291      TXT(", ");
292      ENM(decl->SamplerView.Resource, tgsi_texture_names);
293      TXT(", ");
294      if ((decl->SamplerView.ReturnTypeX == decl->SamplerView.ReturnTypeY) &&
295          (decl->SamplerView.ReturnTypeX == decl->SamplerView.ReturnTypeZ) &&
296          (decl->SamplerView.ReturnTypeX == decl->SamplerView.ReturnTypeW)) {
297         ENM(decl->SamplerView.ReturnTypeX, tgsi_type_names);
298      } else {
299         ENM(decl->SamplerView.ReturnTypeX, tgsi_type_names);
300         TXT(", ");
301         ENM(decl->SamplerView.ReturnTypeY, tgsi_type_names);
302         TXT(", ");
303         ENM(decl->SamplerView.ReturnTypeZ, tgsi_type_names);
304         TXT(", ");
305         ENM(decl->SamplerView.ReturnTypeW, tgsi_type_names);
306      }
307   }
308
309   if (decl->Declaration.Interpolate) {
310      if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT &&
311          decl->Declaration.File == TGSI_FILE_INPUT)
312      {
313         TXT( ", " );
314         ENM( decl->Interp.Interpolate, tgsi_interpolate_names );
315      }
316
317      if (decl->Interp.Centroid) {
318         TXT( ", CENTROID" );
319      }
320
321      if (decl->Interp.CylindricalWrap) {
322         TXT(", CYLWRAP_");
323         if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_X) {
324            CHR('X');
325         }
326         if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Y) {
327            CHR('Y');
328         }
329         if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Z) {
330            CHR('Z');
331         }
332         if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_W) {
333            CHR('W');
334         }
335      }
336   }
337
338   if (decl->Declaration.Invariant) {
339      TXT( ", INVARIANT" );
340   }
341
342
343   if (decl->Declaration.File == TGSI_FILE_IMMEDIATE_ARRAY) {
344      unsigned i;
345      char range_indent[4];
346
347      TXT(" {");
348
349      if (decl->Range.Last < 10)
350         range_indent[0] = '\0';
351      else if (decl->Range.Last < 100) {
352         range_indent[0] = ' ';
353         range_indent[1] = '\0';
354      } else if (decl->Range.Last < 1000) {
355         range_indent[0] = ' ';
356         range_indent[1] = ' ';
357         range_indent[2] = '\0';
358      } else {
359         range_indent[0] = ' ';
360         range_indent[1] = ' ';
361         range_indent[2] = ' ';
362         range_indent[3] = '\0';
363      }
364
365      dump_imm_data(iter, decl->ImmediateData.u,
366                    4, TGSI_IMM_FLOAT32);
367      for(i = 1; i <= decl->Range.Last; ++i) {
368         /* indent by strlen of:
369          *   "DCL IMMX[0..1] {" */
370         CHR('\n');
371         TXT( "                " );
372         TXT( range_indent );
373         dump_imm_data(iter, decl->ImmediateData.u + i,
374                       4, TGSI_IMM_FLOAT32);
375      }
376
377      TXT(" }");
378   }
379
380   EOL();
381
382   return TRUE;
383}
384
385void
386tgsi_dump_declaration(
387   const struct tgsi_full_declaration *decl )
388{
389   struct dump_ctx ctx;
390
391   ctx.dump_printf = dump_ctx_printf;
392
393   iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl );
394}
395
396static boolean
397iter_property(
398   struct tgsi_iterate_context *iter,
399   struct tgsi_full_property *prop )
400{
401   int i;
402   struct dump_ctx *ctx = (struct dump_ctx *)iter;
403
404   TXT( "PROPERTY " );
405   ENM(prop->Property.PropertyName, tgsi_property_names);
406
407   if (prop->Property.NrTokens > 1)
408      TXT(" ");
409
410   for (i = 0; i < prop->Property.NrTokens - 1; ++i) {
411      switch (prop->Property.PropertyName) {
412      case TGSI_PROPERTY_GS_INPUT_PRIM:
413      case TGSI_PROPERTY_GS_OUTPUT_PRIM:
414         ENM(prop->u[i].Data, tgsi_primitive_names);
415         break;
416      case TGSI_PROPERTY_FS_COORD_ORIGIN:
417         ENM(prop->u[i].Data, tgsi_fs_coord_origin_names);
418         break;
419      case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER:
420         ENM(prop->u[i].Data, tgsi_fs_coord_pixel_center_names);
421         break;
422      default:
423         SID( prop->u[i].Data );
424         break;
425      }
426      if (i < prop->Property.NrTokens - 2)
427         TXT( ", " );
428   }
429   EOL();
430
431   return TRUE;
432}
433
434void tgsi_dump_property(
435   const struct tgsi_full_property *prop )
436{
437   struct dump_ctx ctx;
438
439   ctx.dump_printf = dump_ctx_printf;
440
441   iter_property( &ctx.iter, (struct tgsi_full_property *)prop );
442}
443
444static boolean
445iter_immediate(
446   struct tgsi_iterate_context *iter,
447   struct tgsi_full_immediate *imm )
448{
449   struct dump_ctx *ctx = (struct dump_ctx *) iter;
450
451   TXT( "IMM " );
452   ENM( imm->Immediate.DataType, tgsi_immediate_type_names );
453
454   dump_imm_data(iter, imm->u, imm->Immediate.NrTokens - 1,
455                 imm->Immediate.DataType);
456
457   EOL();
458
459   return TRUE;
460}
461
462void
463tgsi_dump_immediate(
464   const struct tgsi_full_immediate *imm )
465{
466   struct dump_ctx ctx;
467
468   ctx.dump_printf = dump_ctx_printf;
469
470   iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm );
471}
472
473static boolean
474iter_instruction(
475   struct tgsi_iterate_context *iter,
476   struct tgsi_full_instruction *inst )
477{
478   struct dump_ctx *ctx = (struct dump_ctx *) iter;
479   uint instno = ctx->instno++;
480   const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode );
481   uint i;
482   boolean first_reg = TRUE;
483
484   INSTID( instno );
485   TXT( ": " );
486
487   ctx->indent -= info->pre_dedent;
488   for(i = 0; (int)i < ctx->indent; ++i)
489      TXT( "  " );
490   ctx->indent += info->post_indent;
491
492   if (inst->Instruction.Predicate) {
493      CHR( '(' );
494
495      if (inst->Predicate.Negate)
496         CHR( '!' );
497
498      TXT( "PRED[" );
499      SID( inst->Predicate.Index );
500      CHR( ']' );
501
502      if (inst->Predicate.SwizzleX != TGSI_SWIZZLE_X ||
503          inst->Predicate.SwizzleY != TGSI_SWIZZLE_Y ||
504          inst->Predicate.SwizzleZ != TGSI_SWIZZLE_Z ||
505          inst->Predicate.SwizzleW != TGSI_SWIZZLE_W) {
506         CHR( '.' );
507         ENM( inst->Predicate.SwizzleX, tgsi_swizzle_names );
508         ENM( inst->Predicate.SwizzleY, tgsi_swizzle_names );
509         ENM( inst->Predicate.SwizzleZ, tgsi_swizzle_names );
510         ENM( inst->Predicate.SwizzleW, tgsi_swizzle_names );
511      }
512
513      TXT( ") " );
514   }
515
516   TXT( info->mnemonic );
517
518   switch (inst->Instruction.Saturate) {
519   case TGSI_SAT_NONE:
520      break;
521   case TGSI_SAT_ZERO_ONE:
522      TXT( "_SAT" );
523      break;
524   case TGSI_SAT_MINUS_PLUS_ONE:
525      TXT( "_SATNV" );
526      break;
527   default:
528      assert( 0 );
529   }
530
531   for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
532      const struct tgsi_full_dst_register *dst = &inst->Dst[i];
533
534      if (!first_reg)
535         CHR( ',' );
536      CHR( ' ' );
537
538      _dump_register_dst( ctx, dst );
539      _dump_writemask( ctx, dst->Register.WriteMask );
540
541      first_reg = FALSE;
542   }
543
544   for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
545      const struct tgsi_full_src_register *src = &inst->Src[i];
546
547      if (!first_reg)
548         CHR( ',' );
549      CHR( ' ' );
550
551      if (src->Register.Negate)
552         CHR( '-' );
553      if (src->Register.Absolute)
554         CHR( '|' );
555
556      _dump_register_src(ctx, src);
557
558      if (src->Register.SwizzleX != TGSI_SWIZZLE_X ||
559          src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
560          src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
561          src->Register.SwizzleW != TGSI_SWIZZLE_W) {
562         CHR( '.' );
563         ENM( src->Register.SwizzleX, tgsi_swizzle_names );
564         ENM( src->Register.SwizzleY, tgsi_swizzle_names );
565         ENM( src->Register.SwizzleZ, tgsi_swizzle_names );
566         ENM( src->Register.SwizzleW, tgsi_swizzle_names );
567      }
568
569      if (src->Register.Absolute)
570         CHR( '|' );
571
572      first_reg = FALSE;
573   }
574
575   if (inst->Instruction.Texture) {
576      TXT( ", " );
577      ENM( inst->Texture.Texture, tgsi_texture_names );
578      for (i = 0; i < inst->Texture.NumOffsets; i++) {
579         TXT( ", " );
580         ENM( inst->TexOffsets[i].File, tgsi_file_names);
581         CHR( '[' );
582         SID( inst->TexOffsets[i].Index );
583         CHR( ']' );
584         CHR( '.' );
585         ENM( inst->TexOffsets[i].SwizzleX, tgsi_swizzle_names);
586         ENM( inst->TexOffsets[i].SwizzleY, tgsi_swizzle_names);
587         ENM( inst->TexOffsets[i].SwizzleZ, tgsi_swizzle_names);
588      }
589   }
590
591   switch (inst->Instruction.Opcode) {
592   case TGSI_OPCODE_IF:
593   case TGSI_OPCODE_ELSE:
594   case TGSI_OPCODE_BGNLOOP:
595   case TGSI_OPCODE_ENDLOOP:
596   case TGSI_OPCODE_CAL:
597      TXT( " :" );
598      UID( inst->Label.Label );
599      break;
600   }
601
602   /* update indentation */
603   if (inst->Instruction.Opcode == TGSI_OPCODE_IF ||
604       inst->Instruction.Opcode == TGSI_OPCODE_ELSE ||
605       inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) {
606      ctx->indentation += indent_spaces;
607   }
608
609   EOL();
610
611   return TRUE;
612}
613
614void
615tgsi_dump_instruction(
616   const struct tgsi_full_instruction *inst,
617   uint instno )
618{
619   struct dump_ctx ctx;
620
621   ctx.instno = instno;
622   ctx.indent = 0;
623   ctx.dump_printf = dump_ctx_printf;
624   ctx.indentation = 0;
625
626   iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst );
627}
628
629static boolean
630prolog(
631   struct tgsi_iterate_context *iter )
632{
633   struct dump_ctx *ctx = (struct dump_ctx *) iter;
634   ENM( iter->processor.Processor, tgsi_processor_type_names );
635   EOL();
636   return TRUE;
637}
638
639void
640tgsi_dump(
641   const struct tgsi_token *tokens,
642   uint flags )
643{
644   struct dump_ctx ctx;
645
646   ctx.iter.prolog = prolog;
647   ctx.iter.iterate_instruction = iter_instruction;
648   ctx.iter.iterate_declaration = iter_declaration;
649   ctx.iter.iterate_immediate = iter_immediate;
650   ctx.iter.iterate_property = iter_property;
651   ctx.iter.epilog = NULL;
652
653   ctx.instno = 0;
654   ctx.indent = 0;
655   ctx.dump_printf = dump_ctx_printf;
656   ctx.indentation = 0;
657
658   tgsi_iterate_shader( tokens, &ctx.iter );
659}
660
661struct str_dump_ctx
662{
663   struct dump_ctx base;
664   char *str;
665   char *ptr;
666   int left;
667};
668
669static void
670str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
671{
672   struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx;
673
674   if(sctx->left > 1) {
675      int written;
676      va_list ap;
677      va_start(ap, format);
678      written = util_vsnprintf(sctx->ptr, sctx->left, format, ap);
679      va_end(ap);
680
681      /* Some complicated logic needed to handle the return value of
682       * vsnprintf:
683       */
684      if (written > 0) {
685         written = MIN2(sctx->left, written);
686         sctx->ptr += written;
687         sctx->left -= written;
688      }
689   }
690}
691
692void
693tgsi_dump_str(
694   const struct tgsi_token *tokens,
695   uint flags,
696   char *str,
697   size_t size)
698{
699   struct str_dump_ctx ctx;
700
701   ctx.base.iter.prolog = prolog;
702   ctx.base.iter.iterate_instruction = iter_instruction;
703   ctx.base.iter.iterate_declaration = iter_declaration;
704   ctx.base.iter.iterate_immediate = iter_immediate;
705   ctx.base.iter.iterate_property = iter_property;
706   ctx.base.iter.epilog = NULL;
707
708   ctx.base.instno = 0;
709   ctx.base.indent = 0;
710   ctx.base.dump_printf = &str_dump_ctx_printf;
711   ctx.base.indentation = 0;
712
713   ctx.str = str;
714   ctx.str[0] = 0;
715   ctx.ptr = str;
716   ctx.left = (int)size;
717
718   tgsi_iterate_shader( tokens, &ctx.base.iter );
719}
720