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