gallium: simplify tgsi_full_immediate struct
[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
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
338 uint i;
339 boolean first_reg = TRUE;
340
341 INSTID( instno );
342 TXT( ": " );
343 TXT( tgsi_get_opcode_info( inst->Instruction.Opcode )->mnemonic );
344
345 switch (inst->Instruction.Saturate) {
346 case TGSI_SAT_NONE:
347 break;
348 case TGSI_SAT_ZERO_ONE:
349 TXT( "_SAT" );
350 break;
351 case TGSI_SAT_MINUS_PLUS_ONE:
352 TXT( "_SATNV" );
353 break;
354 default:
355 assert( 0 );
356 }
357
358 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
359 const struct tgsi_full_dst_register *dst = &inst->FullDstRegisters[i];
360
361 if (!first_reg)
362 CHR( ',' );
363 CHR( ' ' );
364
365 if (dst->DstRegister.Indirect) {
366 _dump_register_ind(
367 ctx,
368 dst->DstRegister.File,
369 dst->DstRegister.Index,
370 dst->DstRegisterInd.File,
371 dst->DstRegisterInd.Index,
372 dst->DstRegisterInd.SwizzleX );
373 }
374 else {
375 _dump_register(
376 ctx,
377 dst->DstRegister.File,
378 dst->DstRegister.Index,
379 dst->DstRegister.Index );
380 }
381 ENM( dst->DstRegisterExtModulate.Modulate, modulate_names );
382 _dump_writemask( ctx, dst->DstRegister.WriteMask );
383
384 first_reg = FALSE;
385 }
386
387 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
388 const struct tgsi_full_src_register *src = &inst->FullSrcRegisters[i];
389
390 if (!first_reg)
391 CHR( ',' );
392 CHR( ' ' );
393
394 if (src->SrcRegisterExtMod.Negate)
395 TXT( "-(" );
396 if (src->SrcRegisterExtMod.Absolute)
397 CHR( '|' );
398 if (src->SrcRegisterExtMod.Scale2X)
399 TXT( "2*(" );
400 if (src->SrcRegisterExtMod.Bias)
401 CHR( '(' );
402 if (src->SrcRegisterExtMod.Complement)
403 TXT( "1-(" );
404 if (src->SrcRegister.Negate)
405 CHR( '-' );
406
407 if (src->SrcRegister.Indirect) {
408 _dump_register_ind(
409 ctx,
410 src->SrcRegister.File,
411 src->SrcRegister.Index,
412 src->SrcRegisterInd.File,
413 src->SrcRegisterInd.Index,
414 src->SrcRegisterInd.SwizzleX );
415 }
416 else {
417 _dump_register(
418 ctx,
419 src->SrcRegister.File,
420 src->SrcRegister.Index,
421 src->SrcRegister.Index );
422 }
423
424 if (src->SrcRegister.SwizzleX != TGSI_SWIZZLE_X ||
425 src->SrcRegister.SwizzleY != TGSI_SWIZZLE_Y ||
426 src->SrcRegister.SwizzleZ != TGSI_SWIZZLE_Z ||
427 src->SrcRegister.SwizzleW != TGSI_SWIZZLE_W) {
428 CHR( '.' );
429 ENM( src->SrcRegister.SwizzleX, swizzle_names );
430 ENM( src->SrcRegister.SwizzleY, swizzle_names );
431 ENM( src->SrcRegister.SwizzleZ, swizzle_names );
432 ENM( src->SrcRegister.SwizzleW, swizzle_names );
433 }
434 if (src->SrcRegisterExtSwz.ExtSwizzleX != TGSI_EXTSWIZZLE_X ||
435 src->SrcRegisterExtSwz.ExtSwizzleY != TGSI_EXTSWIZZLE_Y ||
436 src->SrcRegisterExtSwz.ExtSwizzleZ != TGSI_EXTSWIZZLE_Z ||
437 src->SrcRegisterExtSwz.ExtSwizzleW != TGSI_EXTSWIZZLE_W) {
438 CHR( '.' );
439 if (src->SrcRegisterExtSwz.NegateX)
440 TXT("-");
441 ENM( src->SrcRegisterExtSwz.ExtSwizzleX, extswizzle_names );
442 if (src->SrcRegisterExtSwz.NegateY)
443 TXT("-");
444 ENM( src->SrcRegisterExtSwz.ExtSwizzleY, extswizzle_names );
445 if (src->SrcRegisterExtSwz.NegateZ)
446 TXT("-");
447 ENM( src->SrcRegisterExtSwz.ExtSwizzleZ, extswizzle_names );
448 if (src->SrcRegisterExtSwz.NegateW)
449 TXT("-");
450 ENM( src->SrcRegisterExtSwz.ExtSwizzleW, extswizzle_names );
451 }
452
453 if (src->SrcRegisterExtMod.Complement)
454 CHR( ')' );
455 if (src->SrcRegisterExtMod.Bias)
456 TXT( ")-.5" );
457 if (src->SrcRegisterExtMod.Scale2X)
458 CHR( ')' );
459 if (src->SrcRegisterExtMod.Absolute)
460 CHR( '|' );
461 if (src->SrcRegisterExtMod.Negate)
462 CHR( ')' );
463
464 first_reg = FALSE;
465 }
466
467 if (inst->InstructionExtTexture.Texture != TGSI_TEXTURE_UNKNOWN) {
468 TXT( ", " );
469 ENM( inst->InstructionExtTexture.Texture, texture_names );
470 }
471
472 switch (inst->Instruction.Opcode) {
473 case TGSI_OPCODE_IF:
474 case TGSI_OPCODE_ELSE:
475 case TGSI_OPCODE_BGNLOOP2:
476 case TGSI_OPCODE_ENDLOOP2:
477 case TGSI_OPCODE_CAL:
478 TXT( " :" );
479 UID( inst->InstructionExtLabel.Label );
480 break;
481 }
482
483 EOL();
484
485 return TRUE;
486 }
487
488 void
489 tgsi_dump_instruction(
490 const struct tgsi_full_instruction *inst,
491 uint instno )
492 {
493 struct dump_ctx ctx;
494
495 ctx.instno = instno;
496 ctx.printf = dump_ctx_printf;
497
498 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst );
499 }
500
501 static boolean
502 prolog(
503 struct tgsi_iterate_context *iter )
504 {
505 struct dump_ctx *ctx = (struct dump_ctx *) iter;
506 ENM( iter->processor.Processor, processor_type_names );
507 UID( iter->version.MajorVersion );
508 CHR( '.' );
509 UID( iter->version.MinorVersion );
510 EOL();
511 return TRUE;
512 }
513
514 void
515 tgsi_dump(
516 const struct tgsi_token *tokens,
517 uint flags )
518 {
519 struct dump_ctx ctx;
520
521 ctx.iter.prolog = prolog;
522 ctx.iter.iterate_instruction = iter_instruction;
523 ctx.iter.iterate_declaration = iter_declaration;
524 ctx.iter.iterate_immediate = iter_immediate;
525 ctx.iter.epilog = NULL;
526
527 ctx.instno = 0;
528 ctx.printf = dump_ctx_printf;
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.printf = &str_dump_ctx_printf;
581
582 ctx.str = str;
583 ctx.str[0] = 0;
584 ctx.ptr = str;
585 ctx.left = (int)size;
586
587 tgsi_iterate_shader( tokens, &ctx.base.iter );
588 }