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