tgsi/scan: fix num_inputs/num_outputs for shaders with overlapping arrays
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_dump.c
1 /**************************************************************************
2 *
3 * Copyright 2007-2008 VMware, Inc.
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 VMWARE 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 "util/u_math.h"
33 #include "tgsi_dump.h"
34 #include "tgsi_info.h"
35 #include "tgsi_iterate.h"
36 #include "tgsi_strings.h"
37
38
39 /** Number of spaces to indent for IF/LOOP/etc */
40 static const int indent_spaces = 3;
41
42
43 struct dump_ctx
44 {
45 struct tgsi_iterate_context iter;
46
47 boolean dump_float_as_hex;
48
49 uint instno;
50 uint immno;
51 int indent;
52
53 uint indentation;
54 FILE *file;
55
56 void (*dump_printf)(struct dump_ctx *ctx, const char *format, ...);
57 };
58
59 static void
60 dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
61 {
62 va_list ap;
63 (void)ctx;
64 va_start(ap, format);
65 if (ctx->file)
66 vfprintf(ctx->file, format, ap);
67 else
68 _debug_vprintf(format, ap);
69 va_end(ap);
70 }
71
72 static void
73 dump_enum(
74 struct dump_ctx *ctx,
75 uint e,
76 const char **enums,
77 uint enum_count )
78 {
79 if (e >= enum_count)
80 ctx->dump_printf( ctx, "%u", e );
81 else
82 ctx->dump_printf( ctx, "%s", enums[e] );
83 }
84
85 #define EOL() ctx->dump_printf( ctx, "\n" )
86 #define TXT(S) ctx->dump_printf( ctx, "%s", S )
87 #define CHR(C) ctx->dump_printf( ctx, "%c", C )
88 #define UIX(I) ctx->dump_printf( ctx, "0x%x", I )
89 #define UID(I) ctx->dump_printf( ctx, "%u", I )
90 #define INSTID(I) ctx->dump_printf( ctx, "% 3u", I )
91 #define SID(I) ctx->dump_printf( ctx, "%d", I )
92 #define FLT(F) ctx->dump_printf( ctx, "%10.4f", F )
93 #define DBL(D) ctx->dump_printf( ctx, "%10.8f", D )
94 #define HFLT(F) ctx->dump_printf( ctx, "0x%08x", fui((F)) )
95 #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
96
97 const char *
98 tgsi_swizzle_names[4] =
99 {
100 "x",
101 "y",
102 "z",
103 "w"
104 };
105
106 static void
107 _dump_register_src(
108 struct dump_ctx *ctx,
109 const struct tgsi_full_src_register *src )
110 {
111 TXT(tgsi_file_name(src->Register.File));
112 if (src->Register.Dimension) {
113 if (src->Dimension.Indirect) {
114 CHR( '[' );
115 TXT(tgsi_file_name(src->DimIndirect.File));
116 CHR( '[' );
117 SID( src->DimIndirect.Index );
118 TXT( "]." );
119 ENM( src->DimIndirect.Swizzle, tgsi_swizzle_names );
120 if (src->Dimension.Index != 0) {
121 if (src->Dimension.Index > 0)
122 CHR( '+' );
123 SID( src->Dimension.Index );
124 }
125 CHR( ']' );
126 if (src->DimIndirect.ArrayID) {
127 CHR( '(' );
128 SID( src->DimIndirect.ArrayID );
129 CHR( ')' );
130 }
131 } else {
132 CHR('[');
133 SID(src->Dimension.Index);
134 CHR(']');
135 }
136 }
137 if (src->Register.Indirect) {
138 CHR( '[' );
139 TXT(tgsi_file_name(src->Indirect.File));
140 CHR( '[' );
141 SID( src->Indirect.Index );
142 TXT( "]." );
143 ENM( src->Indirect.Swizzle, tgsi_swizzle_names );
144 if (src->Register.Index != 0) {
145 if (src->Register.Index > 0)
146 CHR( '+' );
147 SID( src->Register.Index );
148 }
149 CHR( ']' );
150 if (src->Indirect.ArrayID) {
151 CHR( '(' );
152 SID( src->Indirect.ArrayID );
153 CHR( ')' );
154 }
155 } else {
156 CHR( '[' );
157 SID( src->Register.Index );
158 CHR( ']' );
159 }
160 }
161
162
163 static void
164 _dump_register_dst(
165 struct dump_ctx *ctx,
166 const struct tgsi_full_dst_register *dst )
167 {
168 TXT(tgsi_file_name(dst->Register.File));
169 if (dst->Register.Dimension) {
170 if (dst->Dimension.Indirect) {
171 CHR( '[' );
172 TXT(tgsi_file_name(dst->DimIndirect.File));
173 CHR( '[' );
174 SID( dst->DimIndirect.Index );
175 TXT( "]." );
176 ENM( dst->DimIndirect.Swizzle, tgsi_swizzle_names );
177 if (dst->Dimension.Index != 0) {
178 if (dst->Dimension.Index > 0)
179 CHR( '+' );
180 SID( dst->Dimension.Index );
181 }
182 CHR( ']' );
183 if (dst->DimIndirect.ArrayID) {
184 CHR( '(' );
185 SID( dst->DimIndirect.ArrayID );
186 CHR( ')' );
187 }
188 } else {
189 CHR('[');
190 SID(dst->Dimension.Index);
191 CHR(']');
192 }
193 }
194 if (dst->Register.Indirect) {
195 CHR( '[' );
196 TXT(tgsi_file_name(dst->Indirect.File));
197 CHR( '[' );
198 SID( dst->Indirect.Index );
199 TXT( "]." );
200 ENM( dst->Indirect.Swizzle, tgsi_swizzle_names );
201 if (dst->Register.Index != 0) {
202 if (dst->Register.Index > 0)
203 CHR( '+' );
204 SID( dst->Register.Index );
205 }
206 CHR( ']' );
207 if (dst->Indirect.ArrayID) {
208 CHR( '(' );
209 SID( dst->Indirect.ArrayID );
210 CHR( ')' );
211 }
212 } else {
213 CHR( '[' );
214 SID( dst->Register.Index );
215 CHR( ']' );
216 }
217 }
218 static void
219 _dump_writemask(
220 struct dump_ctx *ctx,
221 uint writemask )
222 {
223 if (writemask != TGSI_WRITEMASK_XYZW) {
224 CHR( '.' );
225 if (writemask & TGSI_WRITEMASK_X)
226 CHR( 'x' );
227 if (writemask & TGSI_WRITEMASK_Y)
228 CHR( 'y' );
229 if (writemask & TGSI_WRITEMASK_Z)
230 CHR( 'z' );
231 if (writemask & TGSI_WRITEMASK_W)
232 CHR( 'w' );
233 }
234 }
235
236 static void
237 dump_imm_data(struct tgsi_iterate_context *iter,
238 union tgsi_immediate_data *data,
239 unsigned num_tokens,
240 unsigned data_type)
241 {
242 struct dump_ctx *ctx = (struct dump_ctx *)iter;
243 unsigned i ;
244
245 TXT( " {" );
246
247 assert( num_tokens <= 4 );
248 for (i = 0; i < num_tokens; i++) {
249 switch (data_type) {
250 case TGSI_IMM_FLOAT64: {
251 union di d;
252 d.ui = data[i].Uint | (uint64_t)data[i+1].Uint << 32;
253 DBL( d.d );
254 i++;
255 break;
256 }
257 case TGSI_IMM_INT64: {
258 union di d;
259 d.i = data[i].Uint | (uint64_t)data[i+1].Uint << 32;
260 UID( d.i );
261 i++;
262 break;
263 }
264 case TGSI_IMM_UINT64: {
265 union di d;
266 d.ui = data[i].Uint | (uint64_t)data[i+1].Uint << 32;
267 UID( d.ui );
268 i++;
269 break;
270 }
271 case TGSI_IMM_FLOAT32:
272 if (ctx->dump_float_as_hex)
273 HFLT( data[i].Float );
274 else
275 FLT( data[i].Float );
276 break;
277 case TGSI_IMM_UINT32:
278 UID(data[i].Uint);
279 break;
280 case TGSI_IMM_INT32:
281 SID(data[i].Int);
282 break;
283 default:
284 assert( 0 );
285 }
286
287 if (i < num_tokens - 1)
288 TXT( ", " );
289 }
290 TXT( "}" );
291 }
292
293 static boolean
294 iter_declaration(
295 struct tgsi_iterate_context *iter,
296 struct tgsi_full_declaration *decl )
297 {
298 struct dump_ctx *ctx = (struct dump_ctx *)iter;
299 boolean patch = decl->Semantic.Name == TGSI_SEMANTIC_PATCH ||
300 decl->Semantic.Name == TGSI_SEMANTIC_TESSINNER ||
301 decl->Semantic.Name == TGSI_SEMANTIC_TESSOUTER ||
302 decl->Semantic.Name == TGSI_SEMANTIC_PRIMID;
303
304 TXT( "DCL " );
305
306 TXT(tgsi_file_name(decl->Declaration.File));
307
308 /* all geometry shader inputs and non-patch tessellation shader inputs are
309 * two dimensional
310 */
311 if (decl->Declaration.File == TGSI_FILE_INPUT &&
312 (iter->processor.Processor == PIPE_SHADER_GEOMETRY ||
313 (!patch &&
314 (iter->processor.Processor == PIPE_SHADER_TESS_CTRL ||
315 iter->processor.Processor == PIPE_SHADER_TESS_EVAL)))) {
316 TXT("[]");
317 }
318
319 /* all non-patch tess ctrl shader outputs are two dimensional */
320 if (decl->Declaration.File == TGSI_FILE_OUTPUT &&
321 !patch &&
322 iter->processor.Processor == PIPE_SHADER_TESS_CTRL) {
323 TXT("[]");
324 }
325
326 if (decl->Declaration.Dimension) {
327 CHR('[');
328 SID(decl->Dim.Index2D);
329 CHR(']');
330 }
331
332 CHR('[');
333 SID(decl->Range.First);
334 if (decl->Range.First != decl->Range.Last) {
335 TXT("..");
336 SID(decl->Range.Last);
337 }
338 CHR(']');
339
340 _dump_writemask(
341 ctx,
342 decl->Declaration.UsageMask );
343
344 if (decl->Declaration.Array) {
345 TXT( ", ARRAY(" );
346 SID(decl->Array.ArrayID);
347 CHR(')');
348 }
349
350 if (decl->Declaration.Local)
351 TXT( ", LOCAL" );
352
353 if (decl->Declaration.Semantic) {
354 TXT( ", " );
355 ENM( decl->Semantic.Name, tgsi_semantic_names );
356 if (decl->Semantic.Index != 0 ||
357 decl->Semantic.Name == TGSI_SEMANTIC_TEXCOORD ||
358 decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) {
359 CHR( '[' );
360 UID( decl->Semantic.Index );
361 CHR( ']' );
362 }
363 }
364
365 if (decl->Declaration.File == TGSI_FILE_IMAGE) {
366 TXT(", ");
367 ENM(decl->Image.Resource, tgsi_texture_names);
368 TXT(", ");
369 TXT(util_format_name(decl->Image.Format));
370 if (decl->Image.Writable)
371 TXT(", WR");
372 if (decl->Image.Raw)
373 TXT(", RAW");
374 }
375
376 if (decl->Declaration.File == TGSI_FILE_BUFFER) {
377 if (decl->Declaration.Atomic)
378 TXT(", ATOMIC");
379 }
380
381 if (decl->Declaration.File == TGSI_FILE_MEMORY) {
382 switch (decl->Declaration.MemType) {
383 /* Note: ,GLOBAL is optional / the default */
384 case TGSI_MEMORY_TYPE_GLOBAL: TXT(", GLOBAL"); break;
385 case TGSI_MEMORY_TYPE_SHARED: TXT(", SHARED"); break;
386 case TGSI_MEMORY_TYPE_PRIVATE: TXT(", PRIVATE"); break;
387 case TGSI_MEMORY_TYPE_INPUT: TXT(", INPUT"); break;
388 }
389 }
390
391 if (decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {
392 TXT(", ");
393 ENM(decl->SamplerView.Resource, tgsi_texture_names);
394 TXT(", ");
395 if ((decl->SamplerView.ReturnTypeX == decl->SamplerView.ReturnTypeY) &&
396 (decl->SamplerView.ReturnTypeX == decl->SamplerView.ReturnTypeZ) &&
397 (decl->SamplerView.ReturnTypeX == decl->SamplerView.ReturnTypeW)) {
398 ENM(decl->SamplerView.ReturnTypeX, tgsi_return_type_names);
399 } else {
400 ENM(decl->SamplerView.ReturnTypeX, tgsi_return_type_names);
401 TXT(", ");
402 ENM(decl->SamplerView.ReturnTypeY, tgsi_return_type_names);
403 TXT(", ");
404 ENM(decl->SamplerView.ReturnTypeZ, tgsi_return_type_names);
405 TXT(", ");
406 ENM(decl->SamplerView.ReturnTypeW, tgsi_return_type_names);
407 }
408 }
409
410 if (decl->Declaration.Interpolate) {
411 if (iter->processor.Processor == PIPE_SHADER_FRAGMENT &&
412 decl->Declaration.File == TGSI_FILE_INPUT)
413 {
414 TXT( ", " );
415 ENM( decl->Interp.Interpolate, tgsi_interpolate_names );
416 }
417
418 if (decl->Interp.Location != TGSI_INTERPOLATE_LOC_CENTER) {
419 TXT( ", " );
420 ENM( decl->Interp.Location, tgsi_interpolate_locations );
421 }
422
423 if (decl->Interp.CylindricalWrap) {
424 TXT(", CYLWRAP_");
425 if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_X) {
426 CHR('X');
427 }
428 if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Y) {
429 CHR('Y');
430 }
431 if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_Z) {
432 CHR('Z');
433 }
434 if (decl->Interp.CylindricalWrap & TGSI_CYLINDRICAL_WRAP_W) {
435 CHR('W');
436 }
437 }
438 }
439
440 if (decl->Declaration.Invariant) {
441 TXT( ", INVARIANT" );
442 }
443
444 EOL();
445
446 return TRUE;
447 }
448
449 void
450 tgsi_dump_declaration(
451 const struct tgsi_full_declaration *decl )
452 {
453 struct dump_ctx ctx;
454 memset(&ctx, 0, sizeof(ctx));
455
456 ctx.dump_printf = dump_ctx_printf;
457
458 iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl );
459 }
460
461 static boolean
462 iter_property(
463 struct tgsi_iterate_context *iter,
464 struct tgsi_full_property *prop )
465 {
466 unsigned i;
467 struct dump_ctx *ctx = (struct dump_ctx *)iter;
468
469 TXT( "PROPERTY " );
470 ENM(prop->Property.PropertyName, tgsi_property_names);
471
472 if (prop->Property.NrTokens > 1)
473 TXT(" ");
474
475 for (i = 0; i < prop->Property.NrTokens - 1; ++i) {
476 switch (prop->Property.PropertyName) {
477 case TGSI_PROPERTY_GS_INPUT_PRIM:
478 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
479 ENM(prop->u[i].Data, tgsi_primitive_names);
480 break;
481 case TGSI_PROPERTY_FS_COORD_ORIGIN:
482 ENM(prop->u[i].Data, tgsi_fs_coord_origin_names);
483 break;
484 case TGSI_PROPERTY_FS_COORD_PIXEL_CENTER:
485 ENM(prop->u[i].Data, tgsi_fs_coord_pixel_center_names);
486 break;
487 case TGSI_PROPERTY_NEXT_SHADER:
488 ENM(prop->u[i].Data, tgsi_processor_type_names);
489 break;
490 default:
491 SID( prop->u[i].Data );
492 break;
493 }
494 if (i < prop->Property.NrTokens - 2)
495 TXT( ", " );
496 }
497 EOL();
498
499 return TRUE;
500 }
501
502 void tgsi_dump_property(
503 const struct tgsi_full_property *prop )
504 {
505 struct dump_ctx ctx;
506 memset(&ctx, 0, sizeof(ctx));
507
508 ctx.dump_printf = dump_ctx_printf;
509
510 iter_property( &ctx.iter, (struct tgsi_full_property *)prop );
511 }
512
513 static boolean
514 iter_immediate(
515 struct tgsi_iterate_context *iter,
516 struct tgsi_full_immediate *imm )
517 {
518 struct dump_ctx *ctx = (struct dump_ctx *) iter;
519
520 TXT( "IMM[" );
521 SID( ctx->immno++ );
522 TXT( "] " );
523 ENM( imm->Immediate.DataType, tgsi_immediate_type_names );
524
525 dump_imm_data(iter, imm->u, imm->Immediate.NrTokens - 1,
526 imm->Immediate.DataType);
527
528 EOL();
529
530 return TRUE;
531 }
532
533 void
534 tgsi_dump_immediate(
535 const struct tgsi_full_immediate *imm )
536 {
537 struct dump_ctx ctx;
538 memset(&ctx, 0, sizeof(ctx));
539
540 ctx.dump_printf = dump_ctx_printf;
541
542 iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm );
543 }
544
545 static boolean
546 iter_instruction(
547 struct tgsi_iterate_context *iter,
548 struct tgsi_full_instruction *inst )
549 {
550 struct dump_ctx *ctx = (struct dump_ctx *) iter;
551 uint instno = ctx->instno++;
552 const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode );
553 uint i;
554 boolean first_reg = TRUE;
555
556 INSTID( instno );
557 TXT( ": " );
558
559 ctx->indent -= info->pre_dedent;
560 for(i = 0; (int)i < ctx->indent; ++i)
561 TXT( " " );
562 ctx->indent += info->post_indent;
563
564 if (inst->Instruction.Predicate) {
565 CHR( '(' );
566
567 if (inst->Predicate.Negate)
568 CHR( '!' );
569
570 TXT( "PRED[" );
571 SID( inst->Predicate.Index );
572 CHR( ']' );
573
574 if (inst->Predicate.SwizzleX != TGSI_SWIZZLE_X ||
575 inst->Predicate.SwizzleY != TGSI_SWIZZLE_Y ||
576 inst->Predicate.SwizzleZ != TGSI_SWIZZLE_Z ||
577 inst->Predicate.SwizzleW != TGSI_SWIZZLE_W) {
578 CHR( '.' );
579 ENM( inst->Predicate.SwizzleX, tgsi_swizzle_names );
580 ENM( inst->Predicate.SwizzleY, tgsi_swizzle_names );
581 ENM( inst->Predicate.SwizzleZ, tgsi_swizzle_names );
582 ENM( inst->Predicate.SwizzleW, tgsi_swizzle_names );
583 }
584
585 TXT( ") " );
586 }
587
588 TXT( info->mnemonic );
589
590 if (inst->Instruction.Saturate) {
591 TXT( "_SAT" );
592 }
593
594 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
595 const struct tgsi_full_dst_register *dst = &inst->Dst[i];
596
597 if (!first_reg)
598 CHR( ',' );
599 CHR( ' ' );
600
601 _dump_register_dst( ctx, dst );
602 _dump_writemask( ctx, dst->Register.WriteMask );
603
604 first_reg = FALSE;
605 }
606
607 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
608 const struct tgsi_full_src_register *src = &inst->Src[i];
609
610 if (!first_reg)
611 CHR( ',' );
612 CHR( ' ' );
613
614 if (src->Register.Negate)
615 CHR( '-' );
616 if (src->Register.Absolute)
617 CHR( '|' );
618
619 _dump_register_src(ctx, src);
620
621 if (src->Register.SwizzleX != TGSI_SWIZZLE_X ||
622 src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
623 src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
624 src->Register.SwizzleW != TGSI_SWIZZLE_W) {
625 CHR( '.' );
626 ENM( src->Register.SwizzleX, tgsi_swizzle_names );
627 ENM( src->Register.SwizzleY, tgsi_swizzle_names );
628 ENM( src->Register.SwizzleZ, tgsi_swizzle_names );
629 ENM( src->Register.SwizzleW, tgsi_swizzle_names );
630 }
631
632 if (src->Register.Absolute)
633 CHR( '|' );
634
635 first_reg = FALSE;
636 }
637
638 if (inst->Instruction.Texture) {
639 if (!(inst->Instruction.Opcode >= TGSI_OPCODE_SAMPLE &&
640 inst->Instruction.Opcode <= TGSI_OPCODE_GATHER4)) {
641 TXT( ", " );
642 ENM( inst->Texture.Texture, tgsi_texture_names );
643 }
644 for (i = 0; i < inst->Texture.NumOffsets; i++) {
645 TXT( ", " );
646 TXT(tgsi_file_name(inst->TexOffsets[i].File));
647 CHR( '[' );
648 SID( inst->TexOffsets[i].Index );
649 CHR( ']' );
650 CHR( '.' );
651 ENM( inst->TexOffsets[i].SwizzleX, tgsi_swizzle_names);
652 ENM( inst->TexOffsets[i].SwizzleY, tgsi_swizzle_names);
653 ENM( inst->TexOffsets[i].SwizzleZ, tgsi_swizzle_names);
654 }
655 }
656
657 if (inst->Instruction.Memory) {
658 uint32_t qualifier = inst->Memory.Qualifier;
659 while (qualifier) {
660 int bit = ffs(qualifier) - 1;
661 qualifier &= ~(1U << bit);
662 TXT(", ");
663 ENM(bit, tgsi_memory_names);
664 }
665 if (inst->Memory.Texture) {
666 TXT( ", " );
667 ENM( inst->Memory.Texture, tgsi_texture_names );
668 }
669 if (inst->Memory.Format) {
670 TXT( ", " );
671 TXT( util_format_name(inst->Memory.Format) );
672 }
673 }
674
675 switch (inst->Instruction.Opcode) {
676 case TGSI_OPCODE_IF:
677 case TGSI_OPCODE_UIF:
678 case TGSI_OPCODE_ELSE:
679 case TGSI_OPCODE_BGNLOOP:
680 case TGSI_OPCODE_ENDLOOP:
681 case TGSI_OPCODE_CAL:
682 case TGSI_OPCODE_BGNSUB:
683 TXT( " :" );
684 UID( inst->Label.Label );
685 break;
686 }
687
688 /* update indentation */
689 if (inst->Instruction.Opcode == TGSI_OPCODE_IF ||
690 inst->Instruction.Opcode == TGSI_OPCODE_UIF ||
691 inst->Instruction.Opcode == TGSI_OPCODE_ELSE ||
692 inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) {
693 ctx->indentation += indent_spaces;
694 }
695
696 EOL();
697
698 return TRUE;
699 }
700
701 void
702 tgsi_dump_instruction(
703 const struct tgsi_full_instruction *inst,
704 uint instno )
705 {
706 struct dump_ctx ctx;
707 memset(&ctx, 0, sizeof(ctx));
708
709 ctx.instno = instno;
710 ctx.immno = instno;
711 ctx.indent = 0;
712 ctx.dump_printf = dump_ctx_printf;
713 ctx.indentation = 0;
714 ctx.file = NULL;
715
716 iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst );
717 }
718
719 static boolean
720 prolog(
721 struct tgsi_iterate_context *iter )
722 {
723 struct dump_ctx *ctx = (struct dump_ctx *) iter;
724 ENM( iter->processor.Processor, tgsi_processor_type_names );
725 EOL();
726 return TRUE;
727 }
728
729 static void
730 init_dump_ctx(struct dump_ctx *ctx, uint flags)
731 {
732 memset(ctx, 0, sizeof(*ctx));
733
734 ctx->iter.prolog = prolog;
735 ctx->iter.iterate_instruction = iter_instruction;
736 ctx->iter.iterate_declaration = iter_declaration;
737 ctx->iter.iterate_immediate = iter_immediate;
738 ctx->iter.iterate_property = iter_property;
739
740 if (flags & TGSI_DUMP_FLOAT_AS_HEX)
741 ctx->dump_float_as_hex = TRUE;
742 }
743
744 void
745 tgsi_dump_to_file(const struct tgsi_token *tokens, uint flags, FILE *file)
746 {
747 struct dump_ctx ctx;
748 memset(&ctx, 0, sizeof(ctx));
749
750 init_dump_ctx(&ctx, flags);
751
752 ctx.dump_printf = dump_ctx_printf;
753 ctx.file = file;
754
755 tgsi_iterate_shader( tokens, &ctx.iter );
756 }
757
758 void
759 tgsi_dump(const struct tgsi_token *tokens, uint flags)
760 {
761 tgsi_dump_to_file(tokens, flags, NULL);
762 }
763
764 struct str_dump_ctx
765 {
766 struct dump_ctx base;
767 char *str;
768 char *ptr;
769 int left;
770 bool nospace;
771 };
772
773 static void
774 str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
775 {
776 struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx;
777
778 if (!sctx->nospace) {
779 int written;
780 va_list ap;
781 va_start(ap, format);
782 written = util_vsnprintf(sctx->ptr, sctx->left, format, ap);
783 va_end(ap);
784
785 /* Some complicated logic needed to handle the return value of
786 * vsnprintf:
787 */
788 if (written > 0) {
789 if (written >= sctx->left) {
790 sctx->nospace = true;
791 written = sctx->left;
792 }
793 sctx->ptr += written;
794 sctx->left -= written;
795 }
796 }
797 }
798
799 bool
800 tgsi_dump_str(
801 const struct tgsi_token *tokens,
802 uint flags,
803 char *str,
804 size_t size)
805 {
806 struct str_dump_ctx ctx;
807 memset(&ctx, 0, sizeof(ctx));
808
809 init_dump_ctx(&ctx.base, flags);
810
811 ctx.base.dump_printf = &str_dump_ctx_printf;
812
813 ctx.str = str;
814 ctx.str[0] = 0;
815 ctx.ptr = str;
816 ctx.left = (int)size;
817 ctx.nospace = false;
818
819 tgsi_iterate_shader( tokens, &ctx.base.iter );
820
821 return !ctx.nospace;
822 }
823
824 void
825 tgsi_dump_instruction_str(
826 const struct tgsi_full_instruction *inst,
827 uint instno,
828 char *str,
829 size_t size)
830 {
831 struct str_dump_ctx ctx;
832 memset(&ctx, 0, sizeof(ctx));
833
834 ctx.base.instno = instno;
835 ctx.base.immno = instno;
836 ctx.base.indent = 0;
837 ctx.base.dump_printf = &str_dump_ctx_printf;
838 ctx.base.indentation = 0;
839 ctx.base.file = NULL;
840
841 ctx.str = str;
842 ctx.str[0] = 0;
843 ctx.ptr = str;
844 ctx.left = (int)size;
845 ctx.nospace = false;
846
847 iter_instruction( &ctx.base.iter, (struct tgsi_full_instruction *)inst );
848 }