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