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