gallium: Remove TGSI_SEMANTIC_NORMAL.
[mesa/mesa-lb.git] / src / gallium / auxiliary / tgsi / tgsi_dump.c
blobb6df2494143c51a576a77caecd075649112af439
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:
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.
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;
41 struct dump_ctx
43 struct tgsi_iterate_context iter;
45 uint instno;
46 int indent;
48 uint indentation;
50 void (*printf)(struct dump_ctx *ctx, const char *format, ...);
53 static void
54 dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
56 va_list ap;
57 (void)ctx;
58 va_start(ap, format);
59 debug_vprintf(format, ap);
60 va_end(ap);
63 static void
64 dump_enum(
65 struct dump_ctx *ctx,
66 uint e,
67 const char **enums,
68 uint enum_count )
70 if (e >= enum_count)
71 ctx->printf( ctx, "%u", e );
72 else
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[] =
88 "FRAG",
89 "VERT",
90 "GEOM"
93 static const char *file_names[TGSI_FILE_COUNT] =
95 "NULL",
96 "CONST",
97 "IN",
98 "OUT",
99 "TEMP",
100 "SAMP",
101 "ADDR",
102 "IMM",
103 "LOOP",
104 "PRED",
105 "SV"
108 static const char *interpolate_names[] =
110 "CONSTANT",
111 "LINEAR",
112 "PERSPECTIVE"
115 static const char *semantic_names[] =
117 "POSITION",
118 "COLOR",
119 "BCOLOR",
120 "FOG",
121 "PSIZE",
122 "GENERIC",
124 "FACE",
125 "EDGEFLAG",
126 "PRIM_ID",
127 "INSTANCEID"
130 static const char *immediate_type_names[] =
132 "FLT32",
133 "UINT32",
134 "INT32"
137 static const char *swizzle_names[] =
139 "x",
140 "y",
141 "z",
145 static const char *texture_names[] =
147 "UNKNOWN",
148 "1D",
149 "2D",
150 "3D",
151 "CUBE",
152 "RECT",
153 "SHADOW1D",
154 "SHADOW2D",
155 "SHADOWRECT"
158 static const char *property_names[] =
160 "GS_INPUT_PRIMITIVE",
161 "GS_OUTPUT_PRIMITIVE",
162 "GS_MAX_OUTPUT_VERTICES",
163 "FS_COORD_ORIGIN",
164 "FS_COORD_PIXEL_CENTER"
167 static const char *primitive_names[] =
169 "POINTS",
170 "LINES",
171 "LINE_LOOP",
172 "LINE_STRIP",
173 "TRIANGLES",
174 "TRIANGLE_STRIP",
175 "TRIANGLE_FAN",
176 "QUADS",
177 "QUAD_STRIP",
178 "POLYGON"
181 static const char *fs_coord_origin_names[] =
183 "UPPER_LEFT",
184 "LOWER_LEFT"
187 static const char *fs_coord_pixel_center_names[] =
189 "HALF_INTEGER",
190 "INTEGER"
194 static void
195 _dump_register_dst(
196 struct dump_ctx *ctx,
197 uint file,
198 int index)
200 ENM( file, file_names );
202 CHR( '[' );
203 SID( index );
204 CHR( ']' );
208 static void
209 _dump_register_src(
210 struct dump_ctx *ctx,
211 const struct tgsi_full_src_register *src )
213 ENM(src->Register.File, file_names);
214 if (src->Register.Dimension) {
215 CHR('[');
216 SID(src->Dimension.Index);
217 CHR(']');
219 if (src->Register.Indirect) {
220 CHR( '[' );
221 ENM( src->Indirect.File, file_names );
222 CHR( '[' );
223 SID( src->Indirect.Index );
224 TXT( "]." );
225 ENM( src->Indirect.SwizzleX, swizzle_names );
226 if (src->Register.Index != 0) {
227 if (src->Register.Index > 0)
228 CHR( '+' );
229 SID( src->Register.Index );
231 CHR( ']' );
232 } else {
233 CHR( '[' );
234 SID( src->Register.Index );
235 CHR( ']' );
239 static void
240 _dump_register_ind(
241 struct dump_ctx *ctx,
242 uint file,
243 int index,
244 uint ind_file,
245 int ind_index,
246 uint ind_swizzle )
248 ENM( file, file_names );
249 CHR( '[' );
250 ENM( ind_file, file_names );
251 CHR( '[' );
252 SID( ind_index );
253 TXT( "]." );
254 ENM( ind_swizzle, swizzle_names );
255 if (index != 0) {
256 if (index > 0)
257 CHR( '+' );
258 SID( index );
260 CHR( ']' );
263 static void
264 _dump_writemask(
265 struct dump_ctx *ctx,
266 uint writemask )
268 if (writemask != TGSI_WRITEMASK_XYZW) {
269 CHR( '.' );
270 if (writemask & TGSI_WRITEMASK_X)
271 CHR( 'x' );
272 if (writemask & TGSI_WRITEMASK_Y)
273 CHR( 'y' );
274 if (writemask & TGSI_WRITEMASK_Z)
275 CHR( 'z' );
276 if (writemask & TGSI_WRITEMASK_W)
277 CHR( 'w' );
281 static boolean
282 iter_declaration(
283 struct tgsi_iterate_context *iter,
284 struct tgsi_full_declaration *decl )
286 struct dump_ctx *ctx = (struct dump_ctx *)iter;
288 assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT);
289 assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT);
291 TXT( "DCL " );
293 ENM(decl->Declaration.File, file_names);
295 /* all geometry shader inputs are two dimensional */
296 if (decl->Declaration.File == TGSI_FILE_INPUT &&
297 iter->processor.Processor == TGSI_PROCESSOR_GEOMETRY) {
298 TXT("[]");
301 if (decl->Declaration.Dimension) {
302 CHR('[');
303 SID(decl->Dim.Index2D);
304 CHR(']');
307 CHR('[');
308 SID(decl->Range.First);
309 if (decl->Range.First != decl->Range.Last) {
310 TXT("..");
311 SID(decl->Range.Last);
313 CHR(']');
315 _dump_writemask(
316 ctx,
317 decl->Declaration.UsageMask );
319 if (decl->Declaration.Semantic) {
320 TXT( ", " );
321 ENM( decl->Semantic.Name, semantic_names );
322 if (decl->Semantic.Index != 0 ||
323 decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) {
324 CHR( '[' );
325 UID( decl->Semantic.Index );
326 CHR( ']' );
330 if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT &&
331 decl->Declaration.File == TGSI_FILE_INPUT)
333 TXT( ", " );
334 ENM( decl->Declaration.Interpolate, interpolate_names );
337 if (decl->Declaration.Centroid) {
338 TXT( ", CENTROID" );
341 if (decl->Declaration.Invariant) {
342 TXT( ", INVARIANT" );
345 if (decl->Declaration.CylindricalWrap) {
346 TXT(", CYLWRAP_");
347 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_X) {
348 CHR('X');
350 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Y) {
351 CHR('Y');
353 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Z) {
354 CHR('Z');
356 if (decl->Declaration.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_W) {
357 CHR('W');
361 EOL();
363 return TRUE;
366 void
367 tgsi_dump_declaration(
368 const struct tgsi_full_declaration *decl )
370 struct dump_ctx ctx;
372 ctx.printf = dump_ctx_printf;
374 iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl );
377 static boolean
378 iter_property(
379 struct tgsi_iterate_context *iter,
380 struct tgsi_full_property *prop )
382 int i;
383 struct dump_ctx *ctx = (struct dump_ctx *)iter;
385 assert(Elements(property_names) == TGSI_PROPERTY_COUNT);
387 TXT( "PROPERTY " );
388 ENM(prop->Property.PropertyName, property_names);
390 if (prop->Property.NrTokens > 1)
391 TXT(" ");
393 for (i = 0; i < prop->Property.NrTokens - 1; ++i) {
394 switch (prop->Property.PropertyName) {
395 case TGSI_PROPERTY_GS_INPUT_PRIM:
396 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
397 ENM(prop->u[i].Data, primitive_names);
398 break;
399 case TGSI_PROPERTY_FS_COORD_ORIGIN:
400 ENM(prop->u[i].Data, fs_coord_origin_names);
401 break;
402 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER:
403 ENM(prop->u[i].Data, fs_coord_pixel_center_names);
404 break;
405 default:
406 SID( prop->u[i].Data );
407 break;
409 if (i < prop->Property.NrTokens - 2)
410 TXT( ", " );
412 EOL();
414 return TRUE;
417 void tgsi_dump_property(
418 const struct tgsi_full_property *prop )
420 struct dump_ctx ctx;
422 ctx.printf = dump_ctx_printf;
424 iter_property( &ctx.iter, (struct tgsi_full_property *)prop );
427 static boolean
428 iter_immediate(
429 struct tgsi_iterate_context *iter,
430 struct tgsi_full_immediate *imm )
432 struct dump_ctx *ctx = (struct dump_ctx *) iter;
434 uint i;
436 TXT( "IMM " );
437 ENM( imm->Immediate.DataType, immediate_type_names );
439 TXT( " { " );
441 assert( imm->Immediate.NrTokens <= 4 + 1 );
442 for (i = 0; i < imm->Immediate.NrTokens - 1; i++) {
443 switch (imm->Immediate.DataType) {
444 case TGSI_IMM_FLOAT32:
445 FLT( imm->u[i].Float );
446 break;
447 case TGSI_IMM_UINT32:
448 UID(imm->u[i].Uint);
449 break;
450 case TGSI_IMM_INT32:
451 SID(imm->u[i].Int);
452 break;
453 default:
454 assert( 0 );
457 if (i < imm->Immediate.NrTokens - 2)
458 TXT( ", " );
460 TXT( " }" );
462 EOL();
464 return TRUE;
467 void
468 tgsi_dump_immediate(
469 const struct tgsi_full_immediate *imm )
471 struct dump_ctx ctx;
473 ctx.printf = dump_ctx_printf;
475 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm );
478 static boolean
479 iter_instruction(
480 struct tgsi_iterate_context *iter,
481 struct tgsi_full_instruction *inst )
483 struct dump_ctx *ctx = (struct dump_ctx *) iter;
484 uint instno = ctx->instno++;
485 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode );
486 uint i;
487 boolean first_reg = TRUE;
489 INSTID( instno );
490 TXT( ": " );
492 ctx->indent -= info->pre_dedent;
493 for(i = 0; (int)i < ctx->indent; ++i)
494 TXT( " " );
495 ctx->indent += info->post_indent;
497 TXT( info->mnemonic );
499 switch (inst->Instruction.Saturate) {
500 case TGSI_SAT_NONE:
501 break;
502 case TGSI_SAT_ZERO_ONE:
503 TXT( "_SAT" );
504 break;
505 case TGSI_SAT_MINUS_PLUS_ONE:
506 TXT( "_SATNV" );
507 break;
508 default:
509 assert( 0 );
512 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
513 const struct tgsi_full_dst_register *dst = &inst->Dst[i];
515 if (!first_reg)
516 CHR( ',' );
517 CHR( ' ' );
519 if (dst->Register.Indirect) {
520 _dump_register_ind(
521 ctx,
522 dst->Register.File,
523 dst->Register.Index,
524 dst->Indirect.File,
525 dst->Indirect.Index,
526 dst->Indirect.SwizzleX );
528 else {
529 _dump_register_dst(
530 ctx,
531 dst->Register.File,
532 dst->Register.Index );
534 _dump_writemask( ctx, dst->Register.WriteMask );
536 first_reg = FALSE;
539 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
540 const struct tgsi_full_src_register *src = &inst->Src[i];
542 if (!first_reg)
543 CHR( ',' );
544 CHR( ' ' );
546 if (src->Register.Negate)
547 CHR( '-' );
548 if (src->Register.Absolute)
549 CHR( '|' );
551 _dump_register_src(ctx, src);
553 if (src->Register.SwizzleX != TGSI_SWIZZLE_X ||
554 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
555 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
556 src->Register.SwizzleW != TGSI_SWIZZLE_W) {
557 CHR( '.' );
558 ENM( src->Register.SwizzleX, swizzle_names );
559 ENM( src->Register.SwizzleY, swizzle_names );
560 ENM( src->Register.SwizzleZ, swizzle_names );
561 ENM( src->Register.SwizzleW, swizzle_names );
564 if (src->Register.Absolute)
565 CHR( '|' );
567 first_reg = FALSE;
570 if (inst->Instruction.Texture) {
571 TXT( ", " );
572 ENM( inst->Texture.Texture, texture_names );
575 switch (inst->Instruction.Opcode) {
576 case TGSI_OPCODE_IF:
577 case TGSI_OPCODE_ELSE:
578 case TGSI_OPCODE_BGNLOOP:
579 case TGSI_OPCODE_ENDLOOP:
580 case TGSI_OPCODE_CAL:
581 TXT( " :" );
582 UID( inst->Label.Label );
583 break;
586 /* update indentation */
587 if (inst->Instruction.Opcode == TGSI_OPCODE_IF ||
588 inst->Instruction.Opcode == TGSI_OPCODE_ELSE ||
589 inst->Instruction.Opcode == TGSI_OPCODE_BGNFOR ||
590 inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) {
591 ctx->indentation += indent_spaces;
594 EOL();
596 return TRUE;
599 void
600 tgsi_dump_instruction(
601 const struct tgsi_full_instruction *inst,
602 uint instno )
604 struct dump_ctx ctx;
606 ctx.instno = instno;
607 ctx.indent = 0;
608 ctx.printf = dump_ctx_printf;
609 ctx.indentation = 0;
611 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst );
614 static boolean
615 prolog(
616 struct tgsi_iterate_context *iter )
618 struct dump_ctx *ctx = (struct dump_ctx *) iter;
619 ENM( iter->processor.Processor, processor_type_names );
620 EOL();
621 return TRUE;
624 void
625 tgsi_dump(
626 const struct tgsi_token *tokens,
627 uint flags )
629 struct dump_ctx ctx;
631 ctx.iter.prolog = prolog;
632 ctx.iter.iterate_instruction = iter_instruction;
633 ctx.iter.iterate_declaration = iter_declaration;
634 ctx.iter.iterate_immediate = iter_immediate;
635 ctx.iter.iterate_property = iter_property;
636 ctx.iter.epilog = NULL;
638 ctx.instno = 0;
639 ctx.indent = 0;
640 ctx.printf = dump_ctx_printf;
641 ctx.indentation = 0;
643 tgsi_iterate_shader( tokens, &ctx.iter );
646 struct str_dump_ctx
648 struct dump_ctx base;
649 char *str;
650 char *ptr;
651 int left;
654 static void
655 str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
657 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx;
659 if(sctx->left > 1) {
660 int written;
661 va_list ap;
662 va_start(ap, format);
663 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap);
664 va_end(ap);
666 /* Some complicated logic needed to handle the return value of
667 * vsnprintf:
669 if (written > 0) {
670 written = MIN2(sctx->left, written);
671 sctx->ptr += written;
672 sctx->left -= written;
677 void
678 tgsi_dump_str(
679 const struct tgsi_token *tokens,
680 uint flags,
681 char *str,
682 size_t size)
684 struct str_dump_ctx ctx;
686 ctx.base.iter.prolog = prolog;
687 ctx.base.iter.iterate_instruction = iter_instruction;
688 ctx.base.iter.iterate_declaration = iter_declaration;
689 ctx.base.iter.iterate_immediate = iter_immediate;
690 ctx.base.iter.iterate_property = iter_property;
691 ctx.base.iter.epilog = NULL;
693 ctx.base.instno = 0;
694 ctx.base.indent = 0;
695 ctx.base.printf = &str_dump_ctx_printf;
696 ctx.base.indentation = 0;
698 ctx.str = str;
699 ctx.str[0] = 0;
700 ctx.ptr = str;
701 ctx.left = (int)size;
703 tgsi_iterate_shader( tokens, &ctx.base.iter );