Squashed commit of the following:
[profile/ivi/mesa.git] / src / gallium / auxiliary / util / u_dump_state.c
1 /**************************************************************************
2  *
3  * Copyright 2008-2010 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
29 #include "pipe/p_compiler.h"
30 #include "os/os_stream.h"
31 #include "util/u_memory.h"
32 #include "util/u_string.h"
33 #include "util/u_format.h"
34 #include "tgsi/tgsi_dump.h"
35
36 #include "u_dump.h"
37
38
39 /*
40  * Dump primitives
41  */
42
43 static INLINE void
44 util_stream_writef(struct os_stream *stream, const char *format, ...)
45 {
46    static char buf[1024];
47    unsigned len;
48    va_list ap;
49    va_start(ap, format);
50    len = util_vsnprintf(buf, sizeof(buf), format, ap);
51    va_end(ap);
52    os_stream_write(stream, buf, len);
53 }
54
55 static void
56 util_dump_bool(struct os_stream *stream, int value)
57 {
58    util_stream_writef(stream, "%c", value ? '1' : '0');
59 }
60
61 static void
62 util_dump_int(struct os_stream *stream, long long int value)
63 {
64    util_stream_writef(stream, "%lli", value);
65 }
66
67 static void
68 util_dump_uint(struct os_stream *stream, long long unsigned value)
69 {
70    util_stream_writef(stream, "%llu", value);
71 }
72
73 static void
74 util_dump_float(struct os_stream *stream, double value)
75 {
76    util_stream_writef(stream, "%g", value);
77 }
78
79 static void
80 util_dump_string(struct os_stream *stream, const char *str)
81 {
82    os_stream_write_str(stream, "\"");
83    os_stream_write_str(stream, str);
84    os_stream_write_str(stream, "\"");
85 }
86
87 static void
88 util_dump_enum(struct os_stream *stream, const char *value)
89 {
90    os_stream_write_str(stream, value);
91 }
92
93 static void
94 util_dump_array_begin(struct os_stream *stream)
95 {
96    os_stream_write_str(stream, "{");
97 }
98
99 static void
100 util_dump_array_end(struct os_stream *stream)
101 {
102    os_stream_write_str(stream, "}");
103 }
104
105 static void
106 util_dump_elem_begin(struct os_stream *stream)
107 {
108 }
109
110 static void
111 util_dump_elem_end(struct os_stream *stream)
112 {
113    os_stream_write_str(stream, ", ");
114 }
115
116 static void
117 util_dump_struct_begin(struct os_stream *stream, const char *name)
118 {
119    os_stream_write_str(stream, "{");
120 }
121
122 static void
123 util_dump_struct_end(struct os_stream *stream)
124 {
125    os_stream_write_str(stream, "}");
126 }
127
128 static void
129 util_dump_member_begin(struct os_stream *stream, const char *name)
130 {
131    util_stream_writef(stream, "%s = ", name);
132 }
133
134 static void
135 util_dump_member_end(struct os_stream *stream)
136 {
137    os_stream_write_str(stream, ", ");
138 }
139
140 static void
141 util_dump_null(struct os_stream *stream)
142 {
143    os_stream_write_str(stream, "NULL");
144 }
145
146 static void
147 util_dump_ptr(struct os_stream *stream, const void *value)
148 {
149    if(value)
150       util_stream_writef(stream, "0x%08lx", (unsigned long)(uintptr_t)value);
151    else
152       util_dump_null(stream);
153 }
154
155
156 /*
157  * Code saving macros.
158  */
159
160 #define util_dump_arg(_stream, _type, _arg) \
161    do { \
162       util_dump_arg_begin(_stream, #_arg); \
163       util_dump_##_type(_stream, _arg); \
164       util_dump_arg_end(_stream); \
165    } while(0)
166
167 #define util_dump_ret(_stream, _type, _arg) \
168    do { \
169       util_dump_ret_begin(_stream); \
170       util_dump_##_type(_stream, _arg); \
171       util_dump_ret_end(_stream); \
172    } while(0)
173
174 #define util_dump_array(_stream, _type, _obj, _size) \
175    do { \
176       size_t idx; \
177       util_dump_array_begin(_stream); \
178       for(idx = 0; idx < (_size); ++idx) { \
179          util_dump_elem_begin(_stream); \
180          util_dump_##_type(_stream, (_obj)[idx]); \
181          util_dump_elem_end(_stream); \
182       } \
183       util_dump_array_end(_stream); \
184    } while(0)
185
186 #define util_dump_struct_array(_stream, _type, _obj, _size) \
187    do { \
188       size_t idx; \
189       util_dump_array_begin(_stream); \
190       for(idx = 0; idx < (_size); ++idx) { \
191          util_dump_elem_begin(_stream); \
192          util_dump_##_type(_stream, &(_obj)[idx]); \
193          util_dump_elem_end(_stream); \
194       } \
195       util_dump_array_end(_stream); \
196    } while(0)
197
198 #define util_dump_member(_stream, _type, _obj, _member) \
199    do { \
200       util_dump_member_begin(_stream, #_member); \
201       util_dump_##_type(_stream, (_obj)->_member); \
202       util_dump_member_end(_stream); \
203    } while(0)
204
205 #define util_dump_arg_array(_stream, _type, _arg, _size) \
206    do { \
207       util_dump_arg_begin(_stream, #_arg); \
208       util_dump_array(_stream, _type, _arg, _size); \
209       util_dump_arg_end(_stream); \
210    } while(0)
211
212 #define util_dump_member_array(_stream, _type, _obj, _member) \
213    do { \
214       util_dump_member_begin(_stream, #_member); \
215       util_dump_array(_stream, _type, (_obj)->_member, sizeof((_obj)->_member)/sizeof((_obj)->_member[0])); \
216       util_dump_member_end(_stream); \
217    } while(0)
218
219
220
221 /*
222  * Wrappers for enum -> string dumpers.
223  */
224
225
226 static void
227 util_dump_format(struct os_stream *stream, enum pipe_format format)
228 {
229    util_dump_enum(stream, util_format_name(format));
230 }
231
232
233 static void
234 util_dump_enum_blend_factor(struct os_stream *stream, unsigned value)
235 {
236    util_dump_enum(stream, util_dump_blend_factor(value, TRUE));
237 }
238
239 static void
240 util_dump_enum_blend_func(struct os_stream *stream, unsigned value)
241 {
242    util_dump_enum(stream, util_dump_blend_func(value, TRUE));
243 }
244
245 static void
246 util_dump_enum_func(struct os_stream *stream, unsigned value)
247 {
248    util_dump_enum(stream, util_dump_func(value, TRUE));
249 }
250
251
252 /*
253  * Public functions
254  */
255
256
257 void
258 util_dump_template(struct os_stream *stream, const struct pipe_resource *templat)
259 {
260    if(!templat) {
261       util_dump_null(stream);
262       return;
263    }
264
265    util_dump_struct_begin(stream, "pipe_resource");
266
267    util_dump_member(stream, int, templat, target);
268    util_dump_member(stream, format, templat, format);
269
270    util_dump_member_begin(stream, "width");
271    util_dump_uint(stream, templat->width0);
272    util_dump_member_end(stream);
273
274    util_dump_member_begin(stream, "height");
275    util_dump_uint(stream, templat->height0);
276    util_dump_member_end(stream);
277
278    util_dump_member_begin(stream, "depth");
279    util_dump_uint(stream, templat->depth0);
280    util_dump_member_end(stream);
281
282    util_dump_member(stream, uint, templat, last_level);
283    util_dump_member(stream, uint, templat, _usage);
284    util_dump_member(stream, uint, templat, bind);
285    util_dump_member(stream, uint, templat, flags);
286
287    util_dump_struct_end(stream);
288 }
289
290
291 void
292 util_dump_rasterizer_state(struct os_stream *stream, const struct pipe_rasterizer_state *state)
293 {
294    if(!state) {
295       util_dump_null(stream);
296       return;
297    }
298
299    util_dump_struct_begin(stream, "pipe_rasterizer_state");
300
301    util_dump_member(stream, bool, state, flatshade);
302    util_dump_member(stream, bool, state, light_twoside);
303    util_dump_member(stream, uint, state, front_winding);
304    util_dump_member(stream, uint, state, cull_mode);
305    util_dump_member(stream, uint, state, fill_cw);
306    util_dump_member(stream, uint, state, fill_ccw);
307    util_dump_member(stream, bool, state, offset_cw);
308    util_dump_member(stream, bool, state, offset_ccw);
309    util_dump_member(stream, bool, state, scissor);
310    util_dump_member(stream, bool, state, poly_smooth);
311    util_dump_member(stream, bool, state, poly_stipple_enable);
312    util_dump_member(stream, bool, state, point_smooth);
313    util_dump_member(stream, uint, state, sprite_coord_enable);
314    util_dump_member(stream, bool, state, sprite_coord_mode);
315    util_dump_member(stream, bool, state, point_quad_rasterization);
316    util_dump_member(stream, bool, state, point_size_per_vertex);
317    util_dump_member(stream, bool, state, multisample);
318    util_dump_member(stream, bool, state, line_smooth);
319    util_dump_member(stream, bool, state, line_stipple_enable);
320    util_dump_member(stream, uint, state, line_stipple_factor);
321    util_dump_member(stream, uint, state, line_stipple_pattern);
322    util_dump_member(stream, bool, state, line_last_pixel);
323    util_dump_member(stream, bool, state, flatshade_first);
324    util_dump_member(stream, bool, state, gl_rasterization_rules);
325
326    util_dump_member(stream, float, state, line_width);
327    util_dump_member(stream, float, state, point_size);
328    util_dump_member(stream, float, state, offset_units);
329    util_dump_member(stream, float, state, offset_scale);
330
331    util_dump_struct_end(stream);
332 }
333
334
335 void
336 util_dump_poly_stipple(struct os_stream *stream, const struct pipe_poly_stipple *state)
337 {
338    if(!state) {
339       util_dump_null(stream);
340       return;
341    }
342
343    util_dump_struct_begin(stream, "pipe_poly_stipple");
344
345    util_dump_member_begin(stream, "stipple");
346    util_dump_member_array(stream, uint, state, stipple);
347    util_dump_member_end(stream);
348
349    util_dump_struct_end(stream);
350 }
351
352
353 void
354 util_dump_viewport_state(struct os_stream *stream, const struct pipe_viewport_state *state)
355 {
356    if(!state) {
357       util_dump_null(stream);
358       return;
359    }
360
361    util_dump_struct_begin(stream, "pipe_viewport_state");
362
363    util_dump_member_array(stream, float, state, scale);
364    util_dump_member_array(stream, float, state, translate);
365
366    util_dump_struct_end(stream);
367 }
368
369
370 void
371 util_dump_scissor_state(struct os_stream *stream, const struct pipe_scissor_state *state)
372 {
373    if(!state) {
374       util_dump_null(stream);
375       return;
376    }
377
378    util_dump_struct_begin(stream, "pipe_scissor_state");
379
380    util_dump_member(stream, uint, state, minx);
381    util_dump_member(stream, uint, state, miny);
382    util_dump_member(stream, uint, state, maxx);
383    util_dump_member(stream, uint, state, maxy);
384
385    util_dump_struct_end(stream);
386 }
387
388
389 void
390 util_dump_clip_state(struct os_stream *stream, const struct pipe_clip_state *state)
391 {
392    unsigned i;
393
394    if(!state) {
395       util_dump_null(stream);
396       return;
397    }
398
399    util_dump_struct_begin(stream, "pipe_clip_state");
400
401    util_dump_member_begin(stream, "ucp");
402    util_dump_array_begin(stream);
403    for(i = 0; i < PIPE_MAX_CLIP_PLANES; ++i) {
404       util_dump_elem_begin(stream);
405       util_dump_array(stream, float, state->ucp[i], 4);
406       util_dump_elem_end(stream);
407    }
408    util_dump_array_end(stream);
409    util_dump_member_end(stream);
410
411    util_dump_member(stream, uint, state, nr);
412
413    util_dump_struct_end(stream);
414 }
415
416
417 void
418 util_dump_shader_state(struct os_stream *stream, const struct pipe_shader_state *state)
419 {
420    char str[8192];
421
422    if(!state) {
423       util_dump_null(stream);
424       return;
425    }
426
427    tgsi_dump_str(state->tokens, 0, str, sizeof(str));
428
429    util_dump_struct_begin(stream, "pipe_shader_state");
430
431    util_dump_member_begin(stream, "tokens");
432    util_dump_string(stream, str);
433    util_dump_member_end(stream);
434
435    util_dump_struct_end(stream);
436 }
437
438
439 void
440 util_dump_depth_stencil_alpha_state(struct os_stream *stream, const struct pipe_depth_stencil_alpha_state *state)
441 {
442    unsigned i;
443
444    if(!state) {
445       util_dump_null(stream);
446       return;
447    }
448
449    util_dump_struct_begin(stream, "pipe_depth_stencil_alpha_state");
450
451    util_dump_member_begin(stream, "depth");
452    util_dump_struct_begin(stream, "pipe_depth_state");
453    util_dump_member(stream, bool, &state->depth, enabled);
454    if (state->depth.enabled) {
455       util_dump_member(stream, bool, &state->depth, writemask);
456       util_dump_member(stream, enum_func, &state->depth, func);
457    }
458    util_dump_struct_end(stream);
459    util_dump_member_end(stream);
460
461    util_dump_member_begin(stream, "stencil");
462    util_dump_array_begin(stream);
463    for(i = 0; i < Elements(state->stencil); ++i) {
464       util_dump_elem_begin(stream);
465       util_dump_struct_begin(stream, "pipe_stencil_state");
466       util_dump_member(stream, bool, &state->stencil[i], enabled);
467       if (state->stencil[i].enabled) {
468          util_dump_member(stream, enum_func, &state->stencil[i], func);
469          util_dump_member(stream, uint, &state->stencil[i], fail_op);
470          util_dump_member(stream, uint, &state->stencil[i], zpass_op);
471          util_dump_member(stream, uint, &state->stencil[i], zfail_op);
472          util_dump_member(stream, uint, &state->stencil[i], valuemask);
473          util_dump_member(stream, uint, &state->stencil[i], writemask);
474       }
475       util_dump_struct_end(stream);
476       util_dump_elem_end(stream);
477    }
478    util_dump_array_end(stream);
479    util_dump_member_end(stream);
480
481    util_dump_member_begin(stream, "alpha");
482    util_dump_struct_begin(stream, "pipe_alpha_state");
483    util_dump_member(stream, bool, &state->alpha, enabled);
484    if (state->alpha.enabled) {
485       util_dump_member(stream, enum_func, &state->alpha, func);
486       util_dump_member(stream, float, &state->alpha, ref_value);
487    }
488    util_dump_struct_end(stream);
489    util_dump_member_end(stream);
490
491    util_dump_struct_end(stream);
492 }
493
494 void
495 util_dump_rt_blend_state(struct os_stream *stream, const struct pipe_rt_blend_state *state)
496 {
497    util_dump_struct_begin(stream, "pipe_rt_blend_state");
498
499    util_dump_member(stream, uint, state, blend_enable);
500    if (state->blend_enable) {
501       util_dump_member(stream, enum_blend_func, state, rgb_func);
502       util_dump_member(stream, enum_blend_factor, state, rgb_src_factor);
503       util_dump_member(stream, enum_blend_factor, state, rgb_dst_factor);
504
505       util_dump_member(stream, enum_blend_func, state, alpha_func);
506       util_dump_member(stream, enum_blend_factor, state, alpha_src_factor);
507       util_dump_member(stream, enum_blend_factor, state, alpha_dst_factor);
508    }
509
510    util_dump_member(stream, uint, state, colormask);
511
512    util_dump_struct_end(stream);
513 }
514
515 void
516 util_dump_blend_state(struct os_stream *stream, const struct pipe_blend_state *state)
517 {
518    unsigned valid_entries = 1;
519
520    if(!state) {
521       util_dump_null(stream);
522       return;
523    }
524
525    util_dump_struct_begin(stream, "pipe_blend_state");
526
527    util_dump_member(stream, bool, state, dither);
528
529    util_dump_member(stream, bool, state, logicop_enable);
530    if (state->logicop_enable) {
531       util_dump_member(stream, enum_func, state, logicop_func);
532    }
533    else {
534       util_dump_member(stream, bool, state, independent_blend_enable);
535
536       util_dump_member_begin(stream, "rt");
537       if (state->independent_blend_enable)
538          valid_entries = PIPE_MAX_COLOR_BUFS;
539       util_dump_struct_array(stream, rt_blend_state, state->rt, valid_entries);
540       util_dump_member_end(stream);
541    }
542
543    util_dump_struct_end(stream);
544 }
545
546
547 void
548 util_dump_blend_color(struct os_stream *stream, const struct pipe_blend_color *state)
549 {
550    if(!state) {
551       util_dump_null(stream);
552       return;
553    }
554
555    util_dump_struct_begin(stream, "pipe_blend_color");
556
557    util_dump_member_array(stream, float, state, color);
558
559    util_dump_struct_end(stream);
560 }
561
562 void
563 util_dump_stencil_ref(struct os_stream *stream, const struct pipe_stencil_ref *state)
564 {
565    if(!state) {
566       util_dump_null(stream);
567       return;
568    }
569
570    util_dump_struct_begin(stream, "pipe_stencil_ref");
571
572    util_dump_member_array(stream, uint, state, ref_value);
573
574    util_dump_struct_end(stream);
575 }
576
577 void
578 util_dump_framebuffer_state(struct os_stream *stream, const struct pipe_framebuffer_state *state)
579 {
580    util_dump_struct_begin(stream, "pipe_framebuffer_state");
581
582    util_dump_member(stream, uint, state, width);
583    util_dump_member(stream, uint, state, height);
584    util_dump_member(stream, uint, state, nr_cbufs);
585    util_dump_member_array(stream, ptr, state, cbufs);
586    util_dump_member(stream, ptr, state, zsbuf);
587
588    util_dump_struct_end(stream);
589 }
590
591
592 void
593 util_dump_sampler_state(struct os_stream *stream, const struct pipe_sampler_state *state)
594 {
595    if(!state) {
596       util_dump_null(stream);
597       return;
598    }
599
600    util_dump_struct_begin(stream, "pipe_sampler_state");
601
602    util_dump_member(stream, uint, state, wrap_s);
603    util_dump_member(stream, uint, state, wrap_t);
604    util_dump_member(stream, uint, state, wrap_r);
605    util_dump_member(stream, uint, state, min_img_filter);
606    util_dump_member(stream, uint, state, min_mip_filter);
607    util_dump_member(stream, uint, state, mag_img_filter);
608    util_dump_member(stream, uint, state, compare_mode);
609    util_dump_member(stream, enum_func, state, compare_func);
610    util_dump_member(stream, bool, state, normalized_coords);
611    util_dump_member(stream, uint, state, max_anisotropy);
612    util_dump_member(stream, float, state, lod_bias);
613    util_dump_member(stream, float, state, min_lod);
614    util_dump_member(stream, float, state, max_lod);
615    util_dump_member_array(stream, float, state, border_color);
616
617    util_dump_struct_end(stream);
618 }
619
620
621 void
622 util_dump_surface(struct os_stream *stream, const struct pipe_surface *state)
623 {
624    if(!state) {
625       util_dump_null(stream);
626       return;
627    }
628
629    util_dump_struct_begin(stream, "pipe_surface");
630
631    util_dump_member(stream, format, state, format);
632    util_dump_member(stream, uint, state, width);
633    util_dump_member(stream, uint, state, height);
634
635    util_dump_member(stream, uint, state, layout);
636    util_dump_member(stream, uint, state, offset);
637    util_dump_member(stream, uint, state, usage);
638
639    util_dump_member(stream, ptr, state, texture);
640    util_dump_member(stream, uint, state, face);
641    util_dump_member(stream, uint, state, level);
642    util_dump_member(stream, uint, state, zslice);
643
644    util_dump_struct_end(stream);
645 }
646
647
648 void
649 util_dump_transfer(struct os_stream *stream, const struct pipe_transfer *state)
650 {
651    if(!state) {
652       util_dump_null(stream);
653       return;
654    }
655
656    util_dump_struct_begin(stream, "pipe_transfer");
657
658    util_dump_member(stream, ptr, state, resource);
659 //   util_dump_member(stream, uint, state, box);
660
661    util_dump_member(stream, uint, state, stride);
662    util_dump_member(stream, uint, state, slice_stride);
663
664 //   util_dump_member(stream, ptr, state, data);
665
666    util_dump_struct_end(stream);
667 }
668
669
670 void
671 util_dump_vertex_buffer(struct os_stream *stream, const struct pipe_vertex_buffer *state)
672 {
673    if(!state) {
674       util_dump_null(stream);
675       return;
676    }
677
678    util_dump_struct_begin(stream, "pipe_vertex_buffer");
679
680    util_dump_member(stream, uint, state, stride);
681    util_dump_member(stream, uint, state, max_index);
682    util_dump_member(stream, uint, state, buffer_offset);
683    util_dump_member(stream, ptr, state, buffer);
684
685    util_dump_struct_end(stream);
686 }
687
688
689 void
690 util_dump_vertex_element(struct os_stream *stream, const struct pipe_vertex_element *state)
691 {
692    if(!state) {
693       util_dump_null(stream);
694       return;
695    }
696
697    util_dump_struct_begin(stream, "pipe_vertex_element");
698
699    util_dump_member(stream, uint, state, src_offset);
700
701    util_dump_member(stream, uint, state, vertex_buffer_index);
702
703    util_dump_member(stream, format, state, src_format);
704
705    util_dump_struct_end(stream);
706 }