Merge branch 'master' of git+ssh://agd5f@git.freedesktop.org/git/mesa/mesa into r6xx...
[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 struct dump_ctx
37 {
38 struct tgsi_iterate_context iter;
39
40 uint instno;
41
42 void (*printf)(struct dump_ctx *ctx, const char *format, ...);
43 };
44
45 static void
46 dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
47 {
48 va_list ap;
49 (void)ctx;
50 va_start(ap, format);
51 debug_vprintf(format, ap);
52 va_end(ap);
53 }
54
55 static void
56 dump_enum(
57 struct dump_ctx *ctx,
58 uint e,
59 const char **enums,
60 uint enum_count )
61 {
62 if (e >= enum_count)
63 ctx->printf( ctx, "%u", e );
64 else
65 ctx->printf( ctx, "%s", enums[e] );
66 }
67
68 #define EOL() ctx->printf( ctx, "\n" )
69 #define TXT(S) ctx->printf( ctx, "%s", S )
70 #define CHR(C) ctx->printf( ctx, "%c", C )
71 #define UIX(I) ctx->printf( ctx, "0x%x", I )
72 #define UID(I) ctx->printf( ctx, "%u", I )
73 #define INSTID(I) ctx->printf( ctx, "% 3u", I )
74 #define SID(I) ctx->printf( ctx, "%d", I )
75 #define FLT(F) ctx->printf( ctx, "%10.4f", F )
76 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
77
78 static const char *processor_type_names[] =
79 {
80 "FRAG",
81 "VERT",
82 "GEOM"
83 };
84
85 static const char *file_names[] =
86 {
87 "NULL",
88 "CONST",
89 "IN",
90 "OUT",
91 "TEMP",
92 "SAMP",
93 "ADDR",
94 "IMM"
95 };
96
97 static const char *interpolate_names[] =
98 {
99 "CONSTANT",
100 "LINEAR",
101 "PERSPECTIVE"
102 };
103
104 static const char *semantic_names[] =
105 {
106 "POSITION",
107 "COLOR",
108 "BCOLOR",
109 "FOG",
110 "PSIZE",
111 "GENERIC",
112 "NORMAL",
113 "FACE"
114 };
115
116 static const char *immediate_type_names[] =
117 {
118 "FLT32"
119 };
120
121 static const char *swizzle_names[] =
122 {
123 "x",
124 "y",
125 "z",
126 "w"
127 };
128
129 static const char *texture_names[] =
130 {
131 "UNKNOWN",
132 "1D",
133 "2D",
134 "3D",
135 "CUBE",
136 "RECT",
137 "SHADOW1D",
138 "SHADOW2D",
139 "SHADOWRECT"
140 };
141
142 static const char *extswizzle_names[] =
143 {
144 "x",
145 "y",
146 "z",
147 "w",
148 "0",
149 "1"
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 for (i = 0; i < imm->Immediate.NrTokens - 1; i++) {
299 switch (imm->Immediate.DataType) {
300 case TGSI_IMM_FLOAT32:
301 FLT( imm->u.ImmediateFloat32[i].Float );
302 break;
303 default:
304 assert( 0 );
305 }
306
307 if (i < imm->Immediate.NrTokens - 2)
308 TXT( ", " );
309 }
310 TXT( " }" );
311
312 EOL();
313
314 return TRUE;
315 }
316
317 void
318 tgsi_dump_immediate(
319 const struct tgsi_full_immediate *imm )
320 {
321 struct dump_ctx ctx;
322
323 ctx.printf = dump_ctx_printf;
324
325 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm );
326 }
327
328 static boolean
329 iter_instruction(
330 struct tgsi_iterate_context *iter,
331 struct tgsi_full_instruction *inst )
332 {
333 struct dump_ctx *ctx = (struct dump_ctx *) iter;
334 uint instno = ctx->instno++;
335
336 uint i;
337 boolean first_reg = TRUE;
338
339 INSTID( instno );
340 TXT( ": " );
341 TXT( tgsi_get_opcode_info( inst->Instruction.Opcode )->mnemonic );
342
343 switch (inst->Instruction.Saturate) {
344 case TGSI_SAT_NONE:
345 break;
346 case TGSI_SAT_ZERO_ONE:
347 TXT( "_SAT" );
348 break;
349 case TGSI_SAT_MINUS_PLUS_ONE:
350 TXT( "_SATNV" );
351 break;
352 default:
353 assert( 0 );
354 }
355
356 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
357 const struct tgsi_full_dst_register *dst = &inst->FullDstRegisters[i];
358
359 if (!first_reg)
360 CHR( ',' );
361 CHR( ' ' );
362
363 if (dst->DstRegister.Indirect) {
364 _dump_register_ind(
365 ctx,
366 dst->DstRegister.File,
367 dst->DstRegister.Index,
368 dst->DstRegisterInd.File,
369 dst->DstRegisterInd.Index,
370 dst->DstRegisterInd.SwizzleX );
371 }
372 else {
373 _dump_register(
374 ctx,
375 dst->DstRegister.File,
376 dst->DstRegister.Index,
377 dst->DstRegister.Index );
378 }
379 ENM( dst->DstRegisterExtModulate.Modulate, modulate_names );
380 _dump_writemask( ctx, dst->DstRegister.WriteMask );
381
382 first_reg = FALSE;
383 }
384
385 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
386 const struct tgsi_full_src_register *src = &inst->FullSrcRegisters[i];
387
388 if (!first_reg)
389 CHR( ',' );
390 CHR( ' ' );
391
392 if (src->SrcRegisterExtMod.Negate)
393 TXT( "-(" );
394 if (src->SrcRegisterExtMod.Absolute)
395 CHR( '|' );
396 if (src->SrcRegisterExtMod.Scale2X)
397 TXT( "2*(" );
398 if (src->SrcRegisterExtMod.Bias)
399 CHR( '(' );
400 if (src->SrcRegisterExtMod.Complement)
401 TXT( "1-(" );
402 if (src->SrcRegister.Negate)
403 CHR( '-' );
404
405 if (src->SrcRegister.Indirect) {
406 _dump_register_ind(
407 ctx,
408 src->SrcRegister.File,
409 src->SrcRegister.Index,
410 src->SrcRegisterInd.File,
411 src->SrcRegisterInd.Index,
412 src->SrcRegisterInd.SwizzleX );
413 }
414 else {
415 _dump_register(
416 ctx,
417 src->SrcRegister.File,
418 src->SrcRegister.Index,
419 src->SrcRegister.Index );
420 }
421
422 if (src->SrcRegister.SwizzleX != TGSI_SWIZZLE_X ||
423 src->SrcRegister.SwizzleY != TGSI_SWIZZLE_Y ||
424 src->SrcRegister.SwizzleZ != TGSI_SWIZZLE_Z ||
425 src->SrcRegister.SwizzleW != TGSI_SWIZZLE_W) {
426 CHR( '.' );
427 ENM( src->SrcRegister.SwizzleX, swizzle_names );
428 ENM( src->SrcRegister.SwizzleY, swizzle_names );
429 ENM( src->SrcRegister.SwizzleZ, swizzle_names );
430 ENM( src->SrcRegister.SwizzleW, swizzle_names );
431 }
432 if (src->SrcRegisterExtSwz.ExtSwizzleX != TGSI_EXTSWIZZLE_X ||
433 src->SrcRegisterExtSwz.ExtSwizzleY != TGSI_EXTSWIZZLE_Y ||
434 src->SrcRegisterExtSwz.ExtSwizzleZ != TGSI_EXTSWIZZLE_Z ||
435 src->SrcRegisterExtSwz.ExtSwizzleW != TGSI_EXTSWIZZLE_W) {
436 CHR( '.' );
437 if (src->SrcRegisterExtSwz.NegateX)
438 TXT("-");
439 ENM( src->SrcRegisterExtSwz.ExtSwizzleX, extswizzle_names );
440 if (src->SrcRegisterExtSwz.NegateY)
441 TXT("-");
442 ENM( src->SrcRegisterExtSwz.ExtSwizzleY, extswizzle_names );
443 if (src->SrcRegisterExtSwz.NegateZ)
444 TXT("-");
445 ENM( src->SrcRegisterExtSwz.ExtSwizzleZ, extswizzle_names );
446 if (src->SrcRegisterExtSwz.NegateW)
447 TXT("-");
448 ENM( src->SrcRegisterExtSwz.ExtSwizzleW, extswizzle_names );
449 }
450
451 if (src->SrcRegisterExtMod.Complement)
452 CHR( ')' );
453 if (src->SrcRegisterExtMod.Bias)
454 TXT( ")-.5" );
455 if (src->SrcRegisterExtMod.Scale2X)
456 CHR( ')' );
457 if (src->SrcRegisterExtMod.Absolute)
458 CHR( '|' );
459 if (src->SrcRegisterExtMod.Negate)
460 CHR( ')' );
461
462 first_reg = FALSE;
463 }
464
465 if (inst->InstructionExtTexture.Texture != TGSI_TEXTURE_UNKNOWN) {
466 TXT( ", " );
467 ENM( inst->InstructionExtTexture.Texture, texture_names );
468 }
469
470 switch (inst->Instruction.Opcode) {
471 case TGSI_OPCODE_IF:
472 case TGSI_OPCODE_ELSE:
473 case TGSI_OPCODE_BGNLOOP2:
474 case TGSI_OPCODE_ENDLOOP2:
475 case TGSI_OPCODE_CAL:
476 TXT( " :" );
477 UID( inst->InstructionExtLabel.Label );
478 break;
479 }
480
481 EOL();
482
483 return TRUE;
484 }
485
486 void
487 tgsi_dump_instruction(
488 const struct tgsi_full_instruction *inst,
489 uint instno )
490 {
491 struct dump_ctx ctx;
492
493 ctx.instno = instno;
494 ctx.printf = dump_ctx_printf;
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.printf = dump_ctx_printf;
527
528 tgsi_iterate_shader( tokens, &ctx.iter );
529 }
530
531 struct str_dump_ctx
532 {
533 struct dump_ctx base;
534 char *str;
535 char *ptr;
536 int left;
537 };
538
539 static void
540 str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
541 {
542 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx;
543
544 if(sctx->left > 1) {
545 int written;
546 va_list ap;
547 va_start(ap, format);
548 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap);
549 va_end(ap);
550
551 /* Some complicated logic needed to handle the return value of
552 * vsnprintf:
553 */
554 if (written > 0) {
555 written = MIN2(sctx->left, written);
556 sctx->ptr += written;
557 sctx->left -= written;
558 }
559 }
560 }
561
562 void
563 tgsi_dump_str(
564 const struct tgsi_token *tokens,
565 uint flags,
566 char *str,
567 size_t size)
568 {
569 struct str_dump_ctx ctx;
570
571 ctx.base.iter.prolog = prolog;
572 ctx.base.iter.iterate_instruction = iter_instruction;
573 ctx.base.iter.iterate_declaration = iter_declaration;
574 ctx.base.iter.iterate_immediate = iter_immediate;
575 ctx.base.iter.epilog = NULL;
576
577 ctx.base.instno = 0;
578 ctx.base.printf = &str_dump_ctx_printf;
579
580 ctx.str = str;
581 ctx.str[0] = 0;
582 ctx.ptr = str;
583 ctx.left = (int)size;
584
585 tgsi_iterate_shader( tokens, &ctx.base.iter );
586 }