Merge commit 'origin/perrtblend'
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_dump.c
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 */
38 static const int indent_spaces = 3;
39
40
41 struct 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
53 static void
54 dump_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
63 static void
64 dump_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
86 static const char *processor_type_names[] =
87 {
88 "FRAG",
89 "VERT",
90 "GEOM"
91 };
92
93 static 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
108 static const char *interpolate_names[] =
109 {
110 "CONSTANT",
111 "LINEAR",
112 "PERSPECTIVE"
113 };
114
115 static 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
130 static const char *immediate_type_names[] =
131 {
132 "FLT32",
133 "UINT32",
134 "INT32"
135 };
136
137 static const char *swizzle_names[] =
138 {
139 "x",
140 "y",
141 "z",
142 "w"
143 };
144
145 static 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
158 static const char *property_names[] =
159 {
160 "GS_INPUT_PRIMITIVE",
161 "GS_OUTPUT_PRIMITIVE",
162 "GS_MAX_OUTPUT_VERTICES"
163 };
164
165 static 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
180 static void
181 _dump_register_dst(
182 struct dump_ctx *ctx,
183 uint file,
184 int index)
185 {
186 ENM( file, file_names );
187
188 CHR( '[' );
189 SID( index );
190 CHR( ']' );
191 }
192
193
194 static void
195 _dump_register_src(
196 struct dump_ctx *ctx,
197 const struct tgsi_full_src_register *src )
198 {
199 ENM(src->Register.File, file_names);
200 if (src->Register.Dimension) {
201 CHR('[');
202 SID(src->Dimension.Index);
203 CHR(']');
204 }
205 if (src->Register.Indirect) {
206 CHR( '[' );
207 ENM( src->Indirect.File, file_names );
208 CHR( '[' );
209 SID( src->Indirect.Index );
210 TXT( "]." );
211 ENM( src->Indirect.SwizzleX, swizzle_names );
212 if (src->Register.Index != 0) {
213 if (src->Register.Index > 0)
214 CHR( '+' );
215 SID( src->Register.Index );
216 }
217 CHR( ']' );
218 } else {
219 CHR( '[' );
220 SID( src->Register.Index );
221 CHR( ']' );
222 }
223 }
224
225 static void
226 _dump_register_ind(
227 struct dump_ctx *ctx,
228 uint file,
229 int index,
230 uint ind_file,
231 int ind_index,
232 uint ind_swizzle )
233 {
234 ENM( file, file_names );
235 CHR( '[' );
236 ENM( ind_file, file_names );
237 CHR( '[' );
238 SID( ind_index );
239 TXT( "]." );
240 ENM( ind_swizzle, swizzle_names );
241 if (index != 0) {
242 if (index > 0)
243 CHR( '+' );
244 SID( index );
245 }
246 CHR( ']' );
247 }
248
249 static void
250 _dump_writemask(
251 struct dump_ctx *ctx,
252 uint writemask )
253 {
254 if (writemask != TGSI_WRITEMASK_XYZW) {
255 CHR( '.' );
256 if (writemask & TGSI_WRITEMASK_X)
257 CHR( 'x' );
258 if (writemask & TGSI_WRITEMASK_Y)
259 CHR( 'y' );
260 if (writemask & TGSI_WRITEMASK_Z)
261 CHR( 'z' );
262 if (writemask & TGSI_WRITEMASK_W)
263 CHR( 'w' );
264 }
265 }
266
267 static boolean
268 iter_declaration(
269 struct tgsi_iterate_context *iter,
270 struct tgsi_full_declaration *decl )
271 {
272 struct dump_ctx *ctx = (struct dump_ctx *)iter;
273
274 assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT);
275 assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT);
276
277 TXT( "DCL " );
278
279 ENM(decl->Declaration.File, file_names);
280
281 /* all geometry shader inputs are two dimensional */
282 if (decl->Declaration.File == TGSI_FILE_INPUT &&
283 iter->processor.Processor == TGSI_PROCESSOR_GEOMETRY) {
284 TXT("[]");
285 }
286
287 if (decl->Declaration.Dimension) {
288 CHR('[');
289 SID(decl->Dim.Index2D);
290 CHR(']');
291 }
292
293 CHR('[');
294 SID(decl->Range.First);
295 if (decl->Range.First != decl->Range.Last) {
296 TXT("..");
297 SID(decl->Range.Last);
298 }
299 CHR(']');
300
301 _dump_writemask(
302 ctx,
303 decl->Declaration.UsageMask );
304
305 if (decl->Declaration.Semantic) {
306 TXT( ", " );
307 ENM( decl->Semantic.Name, semantic_names );
308 if (decl->Semantic.Index != 0 ||
309 decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) {
310 CHR( '[' );
311 UID( decl->Semantic.Index );
312 CHR( ']' );
313 }
314 }
315
316 if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT &&
317 decl->Declaration.File == TGSI_FILE_INPUT)
318 {
319 TXT( ", " );
320 ENM( decl->Declaration.Interpolate, interpolate_names );
321 }
322
323 if (decl->Declaration.Centroid) {
324 TXT( ", CENTROID" );
325 }
326
327 if (decl->Declaration.Invariant) {
328 TXT( ", INVARIANT" );
329 }
330
331 EOL();
332
333 return TRUE;
334 }
335
336 void
337 tgsi_dump_declaration(
338 const struct tgsi_full_declaration *decl )
339 {
340 struct dump_ctx ctx;
341
342 ctx.printf = dump_ctx_printf;
343
344 iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl );
345 }
346
347 static boolean
348 iter_property(
349 struct tgsi_iterate_context *iter,
350 struct tgsi_full_property *prop )
351 {
352 int i;
353 struct dump_ctx *ctx = (struct dump_ctx *)iter;
354
355 assert(Elements(property_names) == TGSI_PROPERTY_COUNT);
356
357 TXT( "PROPERTY " );
358 ENM(prop->Property.PropertyName, property_names);
359
360 if (prop->Property.NrTokens > 1)
361 TXT(" ");
362
363 for (i = 0; i < prop->Property.NrTokens - 1; ++i) {
364 switch (prop->Property.PropertyName) {
365 case TGSI_PROPERTY_GS_INPUT_PRIM:
366 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
367 ENM(prop->u[i].Data, primitive_names);
368 break;
369 default:
370 SID( prop->u[i].Data );
371 break;
372 }
373 if (i < prop->Property.NrTokens - 2)
374 TXT( ", " );
375 }
376 EOL();
377
378 return TRUE;
379 }
380
381 void tgsi_dump_property(
382 const struct tgsi_full_property *prop )
383 {
384 struct dump_ctx ctx;
385
386 ctx.printf = dump_ctx_printf;
387
388 iter_property( &ctx.iter, (struct tgsi_full_property *)prop );
389 }
390
391 static boolean
392 iter_immediate(
393 struct tgsi_iterate_context *iter,
394 struct tgsi_full_immediate *imm )
395 {
396 struct dump_ctx *ctx = (struct dump_ctx *) iter;
397
398 uint i;
399
400 TXT( "IMM " );
401 ENM( imm->Immediate.DataType, immediate_type_names );
402
403 TXT( " { " );
404
405 assert( imm->Immediate.NrTokens <= 4 + 1 );
406 for (i = 0; i < imm->Immediate.NrTokens - 1; i++) {
407 switch (imm->Immediate.DataType) {
408 case TGSI_IMM_FLOAT32:
409 FLT( imm->u[i].Float );
410 break;
411 case TGSI_IMM_UINT32:
412 UID(imm->u[i].Uint);
413 break;
414 case TGSI_IMM_INT32:
415 SID(imm->u[i].Int);
416 break;
417 default:
418 assert( 0 );
419 }
420
421 if (i < imm->Immediate.NrTokens - 2)
422 TXT( ", " );
423 }
424 TXT( " }" );
425
426 EOL();
427
428 return TRUE;
429 }
430
431 void
432 tgsi_dump_immediate(
433 const struct tgsi_full_immediate *imm )
434 {
435 struct dump_ctx ctx;
436
437 ctx.printf = dump_ctx_printf;
438
439 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm );
440 }
441
442 static boolean
443 iter_instruction(
444 struct tgsi_iterate_context *iter,
445 struct tgsi_full_instruction *inst )
446 {
447 struct dump_ctx *ctx = (struct dump_ctx *) iter;
448 uint instno = ctx->instno++;
449 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode );
450 uint i;
451 boolean first_reg = TRUE;
452
453 INSTID( instno );
454 TXT( ": " );
455
456 ctx->indent -= info->pre_dedent;
457 for(i = 0; (int)i < ctx->indent; ++i)
458 TXT( " " );
459 ctx->indent += info->post_indent;
460
461 TXT( info->mnemonic );
462
463 switch (inst->Instruction.Saturate) {
464 case TGSI_SAT_NONE:
465 break;
466 case TGSI_SAT_ZERO_ONE:
467 TXT( "_SAT" );
468 break;
469 case TGSI_SAT_MINUS_PLUS_ONE:
470 TXT( "_SATNV" );
471 break;
472 default:
473 assert( 0 );
474 }
475
476 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
477 const struct tgsi_full_dst_register *dst = &inst->Dst[i];
478
479 if (!first_reg)
480 CHR( ',' );
481 CHR( ' ' );
482
483 if (dst->Register.Indirect) {
484 _dump_register_ind(
485 ctx,
486 dst->Register.File,
487 dst->Register.Index,
488 dst->Indirect.File,
489 dst->Indirect.Index,
490 dst->Indirect.SwizzleX );
491 }
492 else {
493 _dump_register_dst(
494 ctx,
495 dst->Register.File,
496 dst->Register.Index );
497 }
498 _dump_writemask( ctx, dst->Register.WriteMask );
499
500 first_reg = FALSE;
501 }
502
503 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
504 const struct tgsi_full_src_register *src = &inst->Src[i];
505
506 if (!first_reg)
507 CHR( ',' );
508 CHR( ' ' );
509
510 if (src->Register.Negate)
511 CHR( '-' );
512 if (src->Register.Absolute)
513 CHR( '|' );
514
515 _dump_register_src(ctx, src);
516
517 if (src->Register.SwizzleX != TGSI_SWIZZLE_X ||
518 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
519 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
520 src->Register.SwizzleW != TGSI_SWIZZLE_W) {
521 CHR( '.' );
522 ENM( src->Register.SwizzleX, swizzle_names );
523 ENM( src->Register.SwizzleY, swizzle_names );
524 ENM( src->Register.SwizzleZ, swizzle_names );
525 ENM( src->Register.SwizzleW, swizzle_names );
526 }
527
528 if (src->Register.Absolute)
529 CHR( '|' );
530
531 first_reg = FALSE;
532 }
533
534 if (inst->Instruction.Texture) {
535 TXT( ", " );
536 ENM( inst->Texture.Texture, texture_names );
537 }
538
539 switch (inst->Instruction.Opcode) {
540 case TGSI_OPCODE_IF:
541 case TGSI_OPCODE_ELSE:
542 case TGSI_OPCODE_BGNLOOP:
543 case TGSI_OPCODE_ENDLOOP:
544 case TGSI_OPCODE_CAL:
545 TXT( " :" );
546 UID( inst->Label.Label );
547 break;
548 }
549
550 /* update indentation */
551 if (inst->Instruction.Opcode == TGSI_OPCODE_IF ||
552 inst->Instruction.Opcode == TGSI_OPCODE_ELSE ||
553 inst->Instruction.Opcode == TGSI_OPCODE_BGNFOR ||
554 inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) {
555 ctx->indentation += indent_spaces;
556 }
557
558 EOL();
559
560 return TRUE;
561 }
562
563 void
564 tgsi_dump_instruction(
565 const struct tgsi_full_instruction *inst,
566 uint instno )
567 {
568 struct dump_ctx ctx;
569
570 ctx.instno = instno;
571 ctx.indent = 0;
572 ctx.printf = dump_ctx_printf;
573 ctx.indentation = 0;
574
575 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst );
576 }
577
578 static boolean
579 prolog(
580 struct tgsi_iterate_context *iter )
581 {
582 struct dump_ctx *ctx = (struct dump_ctx *) iter;
583 ENM( iter->processor.Processor, processor_type_names );
584 EOL();
585 return TRUE;
586 }
587
588 void
589 tgsi_dump(
590 const struct tgsi_token *tokens,
591 uint flags )
592 {
593 struct dump_ctx ctx;
594
595 ctx.iter.prolog = prolog;
596 ctx.iter.iterate_instruction = iter_instruction;
597 ctx.iter.iterate_declaration = iter_declaration;
598 ctx.iter.iterate_immediate = iter_immediate;
599 ctx.iter.iterate_property = iter_property;
600 ctx.iter.epilog = NULL;
601
602 ctx.instno = 0;
603 ctx.indent = 0;
604 ctx.printf = dump_ctx_printf;
605 ctx.indentation = 0;
606
607 tgsi_iterate_shader( tokens, &ctx.iter );
608 }
609
610 struct str_dump_ctx
611 {
612 struct dump_ctx base;
613 char *str;
614 char *ptr;
615 int left;
616 };
617
618 static void
619 str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
620 {
621 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx;
622
623 if(sctx->left > 1) {
624 int written;
625 va_list ap;
626 va_start(ap, format);
627 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap);
628 va_end(ap);
629
630 /* Some complicated logic needed to handle the return value of
631 * vsnprintf:
632 */
633 if (written > 0) {
634 written = MIN2(sctx->left, written);
635 sctx->ptr += written;
636 sctx->left -= written;
637 }
638 }
639 }
640
641 void
642 tgsi_dump_str(
643 const struct tgsi_token *tokens,
644 uint flags,
645 char *str,
646 size_t size)
647 {
648 struct str_dump_ctx ctx;
649
650 ctx.base.iter.prolog = prolog;
651 ctx.base.iter.iterate_instruction = iter_instruction;
652 ctx.base.iter.iterate_declaration = iter_declaration;
653 ctx.base.iter.iterate_immediate = iter_immediate;
654 ctx.base.iter.iterate_property = iter_property;
655 ctx.base.iter.epilog = NULL;
656
657 ctx.base.instno = 0;
658 ctx.base.indent = 0;
659 ctx.base.printf = &str_dump_ctx_printf;
660 ctx.base.indentation = 0;
661
662 ctx.str = str;
663 ctx.str[0] = 0;
664 ctx.ptr = str;
665 ctx.left = (int)size;
666
667 tgsi_iterate_shader( tokens, &ctx.base.iter );
668 }