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