summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/ddebug/dd_draw.c
blob: 45e4e10d500d89f0734f04efa6437eb51471c4d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/**************************************************************************
 *
 * Copyright 2015 Advanced Micro Devices, Inc.
 * Copyright 2008 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "dd_pipe.h"

#include "util/u_dump.h"
#include "util/u_format.h"
#include "tgsi/tgsi_scan.h"


enum call_type
{
   CALL_DRAW_VBO,
   CALL_RESOURCE_COPY_REGION,
   CALL_BLIT,
   CALL_FLUSH_RESOURCE,
   CALL_CLEAR,
   CALL_CLEAR_BUFFER,
   CALL_CLEAR_RENDER_TARGET,
   CALL_CLEAR_DEPTH_STENCIL,
};

struct call_resource_copy_region
{
   struct pipe_resource *dst;
   unsigned dst_level;
   unsigned dstx, dsty, dstz;
   struct pipe_resource *src;
   unsigned src_level;
   const struct pipe_box *src_box;
};

struct call_clear
{
   unsigned buffers;
   const union pipe_color_union *color;
   double depth;
   unsigned stencil;
};

struct call_clear_buffer
{
   struct pipe_resource *res;
   unsigned offset;
   unsigned size;
   const void *clear_value;
   int clear_value_size;
};

struct dd_call
{
   enum call_type type;

   union {
      struct pipe_draw_info draw_vbo;
      struct call_resource_copy_region resource_copy_region;
      struct pipe_blit_info blit;
      struct pipe_resource *flush_resource;
      struct call_clear clear;
      struct call_clear_buffer clear_buffer;
   } info;
};

static FILE *
dd_get_file_stream(struct dd_context *dctx)
{
   struct dd_screen *dscreen = dd_screen(dctx->base.screen);
   struct pipe_screen *screen = dctx->pipe->screen;
   FILE *f = dd_get_debug_file(dscreen->verbose);
   if (!f)
      return NULL;

   fprintf(f, "Driver vendor: %s\n", screen->get_vendor(screen));
   fprintf(f, "Device vendor: %s\n", screen->get_device_vendor(screen));
   fprintf(f, "Device name: %s\n\n", screen->get_name(screen));
   return f;
}

static void
dd_close_file_stream(FILE *f)
{
   fclose(f);
}

static unsigned
dd_num_active_viewports(struct dd_context *dctx)
{
   struct tgsi_shader_info info;
   const struct tgsi_token *tokens;

   if (dctx->shaders[PIPE_SHADER_GEOMETRY])
      tokens = dctx->shaders[PIPE_SHADER_GEOMETRY]->state.shader.tokens;
   else if (dctx->shaders[PIPE_SHADER_TESS_EVAL])
      tokens = dctx->shaders[PIPE_SHADER_TESS_EVAL]->state.shader.tokens;
   else if (dctx->shaders[PIPE_SHADER_VERTEX])
      tokens = dctx->shaders[PIPE_SHADER_VERTEX]->state.shader.tokens;
   else
      return 1;

   tgsi_scan_shader(tokens, &info);
   return info.writes_viewport_index ? PIPE_MAX_VIEWPORTS : 1;
}

#define COLOR_RESET	"\033[0m"
#define COLOR_SHADER	"\033[1;32m"
#define COLOR_STATE	"\033[1;33m"

#define DUMP(name, var) do { \
   fprintf(f, COLOR_STATE #name ": " COLOR_RESET); \
   util_dump_##name(f, var); \
   fprintf(f, "\n"); \
} while(0)

#define DUMP_I(name, var, i) do { \
   fprintf(f, COLOR_STATE #name " %i: " COLOR_RESET, i); \
   util_dump_##name(f, var); \
   fprintf(f, "\n"); \
} while(0)

#define DUMP_M(name, var, member) do { \
   fprintf(f, "  " #member ": "); \
   util_dump_##name(f, (var)->member); \
   fprintf(f, "\n"); \
} while(0)

#define DUMP_M_ADDR(name, var, member) do { \
   fprintf(f, "  " #member ": "); \
   util_dump_##name(f, &(var)->member); \
   fprintf(f, "\n"); \
} while(0)

static void
print_named_value(FILE *f, const char *name, int value)
{
   fprintf(f, COLOR_STATE "%s" COLOR_RESET " = %i\n", name, value);
}

static void
print_named_xvalue(FILE *f, const char *name, int value)
{
   fprintf(f, COLOR_STATE "%s" COLOR_RESET " = 0x%08x\n", name, value);
}

static void
util_dump_uint(FILE *f, unsigned i)
{
   fprintf(f, "%u", i);
}

static void
util_dump_hex(FILE *f, unsigned i)
{
   fprintf(f, "0x%x", i);
}

static void
util_dump_double(FILE *f, double d)
{
   fprintf(f, "%f", d);
}

static void
util_dump_format(FILE *f, enum pipe_format format)
{
   fprintf(f, "%s", util_format_name(format));
}

static void
util_dump_color_union(FILE *f, const union pipe_color_union *color)
{
   fprintf(f, "{f = {%f, %f, %f, %f}, ui = {%u, %u, %u, %u}",
           color->f[0], color->f[1], color->f[2], color->f[3],
           color->ui[0], color->ui[1], color->ui[2], color->ui[3]);
}

static void
util_dump_query(FILE *f, struct dd_query *query)
{
   if (query->type >= PIPE_QUERY_DRIVER_SPECIFIC)
      fprintf(f, "PIPE_QUERY_DRIVER_SPECIFIC + %i",
              query->type - PIPE_QUERY_DRIVER_SPECIFIC);
   else
      fprintf(f, "%s", util_dump_query_type(query->type, false));
}

static void
dd_dump_render_condition(struct dd_context *dctx, FILE *f)
{
   if (dctx->render_cond.query) {
      fprintf(f, "render condition:\n");
      DUMP_M(query, &dctx->render_cond, query);
      DUMP_M(uint, &dctx->render_cond, condition);
      DUMP_M(uint, &dctx->render_cond, mode);
      fprintf(f, "\n");
   }
}

static void
dd_dump_draw_vbo(struct dd_context *dctx, struct pipe_draw_info *info, FILE *f)
{
   int sh, i;
   const char *shader_str[PIPE_SHADER_TYPES];

   shader_str[PIPE_SHADER_VERTEX] = "VERTEX";
   shader_str[PIPE_SHADER_TESS_CTRL] = "TESS_CTRL";
   shader_str[PIPE_SHADER_TESS_EVAL] = "TESS_EVAL";
   shader_str[PIPE_SHADER_GEOMETRY] = "GEOMETRY";
   shader_str[PIPE_SHADER_FRAGMENT] = "FRAGMENT";
   shader_str[PIPE_SHADER_COMPUTE] = "COMPUTE";

   DUMP(draw_info, info);
   if (info->indexed) {
      DUMP(index_buffer, &dctx->index_buffer);
      if (dctx->index_buffer.buffer)
         DUMP_M(resource, &dctx->index_buffer, buffer);
   }
   if (info->count_from_stream_output)
      DUMP_M(stream_output_target, info,
             count_from_stream_output);
   if (info->indirect)
      DUMP_M(resource, info, indirect);
   fprintf(f, "\n");

   /* TODO: dump active queries */

   dd_dump_render_condition(dctx, f);

   for (i = 0; i < PIPE_MAX_ATTRIBS; i++)
      if (dctx->vertex_buffers[i].buffer ||
          dctx->vertex_buffers[i].user_buffer) {
         DUMP_I(vertex_buffer, &dctx->vertex_buffers[i], i);
         if (dctx->vertex_buffers[i].buffer)
            DUMP_M(resource, &dctx->vertex_buffers[i], buffer);
      }

   if (dctx->velems) {
      print_named_value(f, "num vertex elements",
                        dctx->velems->state.velems.count);
      for (i = 0; i < dctx->velems->state.velems.count; i++) {
         fprintf(f, "  ");
         DUMP_I(vertex_element, &dctx->velems->state.velems.velems[i], i);
      }
   }

   print_named_value(f, "num stream output targets", dctx->num_so_targets);
   for (i = 0; i < dctx->num_so_targets; i++)
      if (dctx->so_targets[i]) {
         DUMP_I(stream_output_target, dctx->so_targets[i], i);
         DUMP_M(resource, dctx->so_targets[i], buffer);
         fprintf(f, "  offset = %i\n", dctx->so_offsets[i]);
      }

   fprintf(f, "\n");
   for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
      if (sh == PIPE_SHADER_COMPUTE)
         continue;

      if (sh == PIPE_SHADER_TESS_CTRL &&
          !dctx->shaders[PIPE_SHADER_TESS_CTRL] &&
          dctx->shaders[PIPE_SHADER_TESS_EVAL])
         fprintf(f, "tess_state: {default_outer_level = {%f, %f, %f, %f}, "
                 "default_inner_level = {%f, %f}}\n",
                 dctx->tess_default_levels[0],
                 dctx->tess_default_levels[1],
                 dctx->tess_default_levels[2],
                 dctx->tess_default_levels[3],
                 dctx->tess_default_levels[4],
                 dctx->tess_default_levels[5]);

      if (sh == PIPE_SHADER_FRAGMENT)
         if (dctx->rs) {
            unsigned num_viewports = dd_num_active_viewports(dctx);

            if (dctx->rs->state.rs.clip_plane_enable)
               DUMP(clip_state, &dctx->clip_state);

            for (i = 0; i < num_viewports; i++)
               DUMP_I(viewport_state, &dctx->viewports[i], i);

            if (dctx->rs->state.rs.scissor)
               for (i = 0; i < num_viewports; i++)
                  DUMP_I(scissor_state, &dctx->scissors[i], i);

            DUMP(rasterizer_state, &dctx->rs->state.rs);

            if (dctx->rs->state.rs.poly_stipple_enable)
               DUMP(poly_stipple, &dctx->polygon_stipple);
            fprintf(f, "\n");
         }

      if (!dctx->shaders[sh])
         continue;

      fprintf(f, COLOR_SHADER "begin shader: %s" COLOR_RESET "\n", shader_str[sh]);
      DUMP(shader_state, &dctx->shaders[sh]->state.shader);

      for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++)
         if (dctx->constant_buffers[sh][i].buffer ||
             dctx->constant_buffers[sh][i].user_buffer) {
            DUMP_I(constant_buffer, &dctx->constant_buffers[sh][i], i);
            if (dctx->constant_buffers[sh][i].buffer)
               DUMP_M(resource, &dctx->constant_buffers[sh][i], buffer);
         }

      for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
         if (dctx->sampler_states[sh][i])
            DUMP_I(sampler_state, &dctx->sampler_states[sh][i]->state.sampler, i);

      for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
         if (dctx->sampler_views[sh][i]) {
            DUMP_I(sampler_view, dctx->sampler_views[sh][i], i);
            DUMP_M(resource, dctx->sampler_views[sh][i], texture);
         }

      /* TODO: print shader images */
      /* TODO: print shader buffers */

      fprintf(f, COLOR_SHADER "end shader: %s" COLOR_RESET "\n\n", shader_str[sh]);
   }

   if (dctx->dsa)
      DUMP(depth_stencil_alpha_state, &dctx->dsa->state.dsa);
   DUMP(stencil_ref, &dctx->stencil_ref);

   if (dctx->blend)
      DUMP(blend_state, &dctx->blend->state.blend);
   DUMP(blend_color, &dctx->blend_color);

   print_named_value(f, "min_samples", dctx->min_samples);
   print_named_xvalue(f, "sample_mask", dctx->sample_mask);
   fprintf(f, "\n");

   DUMP(framebuffer_state, &dctx->framebuffer_state);
   for (i = 0; i < dctx->framebuffer_state.nr_cbufs; i++)
      if (dctx->framebuffer_state.cbufs[i]) {
         fprintf(f, "  " COLOR_STATE "cbufs[%i]:" COLOR_RESET "\n    ", i);
         DUMP(surface, dctx->framebuffer_state.cbufs[i]);
         fprintf(f, "    ");
         DUMP(resource, dctx->framebuffer_state.cbufs[i]->texture);
      }
   if (dctx->framebuffer_state.zsbuf) {
      fprintf(f, "  " COLOR_STATE "zsbuf:" COLOR_RESET "\n    ");
      DUMP(surface, dctx->framebuffer_state.zsbuf);
      fprintf(f, "    ");
      DUMP(resource, dctx->framebuffer_state.zsbuf->texture);
   }
   fprintf(f, "\n");
}

static void
dd_dump_resource_copy_region(struct dd_context *dctx,
                             struct call_resource_copy_region *info,
                             FILE *f)
{
   fprintf(f, "%s:\n", __func__+8);
   DUMP_M(resource, info, dst);
   DUMP_M(uint, info, dst_level);
   DUMP_M(uint, info, dstx);
   DUMP_M(uint, info, dsty);
   DUMP_M(uint, info, dstz);
   DUMP_M(resource, info, src);
   DUMP_M(uint, info, src_level);
   DUMP_M(box, info, src_box);
}

static void
dd_dump_blit(struct dd_context *dctx, struct pipe_blit_info *info, FILE *f)
{
   fprintf(f, "%s:\n", __func__+8);
   DUMP_M(resource, info, dst.resource);
   DUMP_M(uint, info, dst.level);
   DUMP_M_ADDR(box, info, dst.box);
   DUMP_M(format, info, dst.format);

   DUMP_M(resource, info, src.resource);
   DUMP_M(uint, info, src.level);
   DUMP_M_ADDR(box, info, src.box);
   DUMP_M(format, info, src.format);

   DUMP_M(hex, info, mask);
   DUMP_M(uint, info, filter);
   DUMP_M(uint, info, scissor_enable);
   DUMP_M_ADDR(scissor_state, info, scissor);
   DUMP_M(uint, info, render_condition_enable);

   if (info->render_condition_enable)
      dd_dump_render_condition(dctx, f);
}

static void
dd_dump_flush_resource(struct dd_context *dctx, struct pipe_resource *res,
                       FILE *f)
{
   fprintf(f, "%s:\n", __func__+8);
   DUMP(resource, res);
}

static void
dd_dump_clear(struct dd_context *dctx, struct call_clear *info, FILE *f)
{
   fprintf(f, "%s:\n", __func__+8);
   DUMP_M(uint, info, buffers);
   DUMP_M(color_union, info, color);
   DUMP_M(double, info, depth);
   DUMP_M(hex, info, stencil);
}

static void
dd_dump_clear_buffer(struct dd_context *dctx, struct call_clear_buffer *info,
                     FILE *f)
{
   int i;
   const char *value = (const char*)info->clear_value;

   fprintf(f, "%s:\n", __func__+8);
   DUMP_M(resource, info, res);
   DUMP_M(uint, info, offset);
   DUMP_M(uint, info, size);
   DUMP_M(uint, info, clear_value_size);

   fprintf(f, "  clear_value:");
   for (i = 0; i < info->clear_value_size; i++)
      fprintf(f, " %02x", value[i]);
   fprintf(f, "\n");
}

static void
dd_dump_clear_render_target(struct dd_context *dctx, FILE *f)
{
   fprintf(f, "%s:\n", __func__+8);
   /* TODO */
}

static void
dd_dump_clear_depth_stencil(struct dd_context *dctx, FILE *f)
{
   fprintf(f, "%s:\n", __func__+8);
   /* TODO */
}

static void
dd_dump_driver_state(struct dd_context *dctx, FILE *f, unsigned flags)
{
   if (dctx->pipe->dump_debug_state) {
	   fprintf(f,"\n\n**************************************************"
		     "***************************\n");
	   fprintf(f, "Driver-specific state:\n\n");
	   dctx->pipe->dump_debug_state(dctx->pipe, f, flags);
   }
}

static void
dd_dump_call(struct dd_context *dctx, struct dd_call *call, unsigned flags)
{
   FILE *f = dd_get_file_stream(dctx);

   if (!f)
      return;

   switch (call->type) {
   case CALL_DRAW_VBO:
      dd_dump_draw_vbo(dctx, &call->info.draw_vbo, f);
      break;
   case CALL_RESOURCE_COPY_REGION:
      dd_dump_resource_copy_region(dctx, &call->info.resource_copy_region, f);
      break;
   case CALL_BLIT:
      dd_dump_blit(dctx, &call->info.blit, f);
      break;
   case CALL_FLUSH_RESOURCE:
      dd_dump_flush_resource(dctx, call->info.flush_resource, f);
      break;
   case CALL_CLEAR:
      dd_dump_clear(dctx, &call->info.clear, f);
      break;
   case CALL_CLEAR_BUFFER:
      dd_dump_clear_buffer(dctx, &call->info.clear_buffer, f);
      break;
   case CALL_CLEAR_RENDER_TARGET:
      dd_dump_clear_render_target(dctx, f);
      break;
   case CALL_CLEAR_DEPTH_STENCIL:
      dd_dump_clear_depth_stencil(dctx, f);
   }

   dd_dump_driver_state(dctx, f, flags);
   dd_close_file_stream(f);
}

static void
dd_kill_process(void)
{
   sync();
   fprintf(stderr, "dd: Aborting the process...\n");
   fflush(stdout);
   fflush(stderr);
   abort();
}

static bool
dd_flush_and_check_hang(struct dd_context *dctx,
                        struct pipe_fence_handle **flush_fence,
                        unsigned flush_flags)
{
   struct pipe_fence_handle *fence = NULL;
   struct pipe_context *pipe = dctx->pipe;
   struct pipe_screen *screen = pipe->screen;
   uint64_t timeout_ms = dd_screen(dctx->base.screen)->timeout_ms;
   bool idle;

   assert(timeout_ms > 0);

   pipe->flush(pipe, &fence, flush_flags);
   if (flush_fence)
      screen->fence_reference(screen, flush_fence, fence);
   if (!fence)
      return false;

   idle = screen->fence_finish(screen, fence, timeout_ms * 1000000);
   screen->fence_reference(screen, &fence, NULL);
   if (!idle)
      fprintf(stderr, "dd: GPU hang detected!\n");
   return !idle;
}

static void
dd_flush_and_handle_hang(struct dd_context *dctx,
                         struct pipe_fence_handle **fence, unsigned flags,
                         const char *cause)
{
   if (dd_flush_and_check_hang(dctx, fence, flags)) {
      FILE *f = dd_get_file_stream(dctx);

      if (f) {
         fprintf(f, "dd: %s.\n", cause);
         dd_dump_driver_state(dctx, f, PIPE_DEBUG_DEVICE_IS_HUNG);
         dd_close_file_stream(f);
      }

      /* Terminate the process to prevent future hangs. */
      dd_kill_process();
   }
}

static void
dd_context_flush(struct pipe_context *_pipe,
                 struct pipe_fence_handle **fence, unsigned flags)
{
   struct dd_context *dctx = dd_context(_pipe);
   struct pipe_context *pipe = dctx->pipe;

   switch (dd_screen(dctx->base.screen)->mode) {
   case DD_DETECT_HANGS:
      dd_flush_and_handle_hang(dctx, fence, flags,
                               "GPU hang detected in pipe->flush()");
      break;
   case DD_DUMP_ALL_CALLS:
      pipe->flush(pipe, fence, flags);
      break;
   default:
      assert(0);
   }
}

static void
dd_before_draw(struct dd_context *dctx)
{
   struct dd_screen *dscreen = dd_screen(dctx->base.screen);

   if (dscreen->mode == DD_DETECT_HANGS &&
       !dscreen->no_flush &&
       dctx->num_draw_calls >= dscreen->skip_count)
      dd_flush_and_handle_hang(dctx, NULL, 0,
                               "GPU hang most likely caused by internal "
                               "driver commands");
}

static void
dd_after_draw(struct dd_context *dctx, struct dd_call *call)
{
   struct dd_screen *dscreen = dd_screen(dctx->base.screen);
   struct pipe_context *pipe = dctx->pipe;

   if (dctx->num_draw_calls >= dscreen->skip_count) {
      switch (dscreen->mode) {
      case DD_DETECT_HANGS:
         if (!dscreen->no_flush &&
            dd_flush_and_check_hang(dctx, NULL, 0)) {
            dd_dump_call(dctx, call, PIPE_DEBUG_DEVICE_IS_HUNG);

            /* Terminate the process to prevent future hangs. */
            dd_kill_process();
         }
         break;
      case DD_DUMP_ALL_CALLS:
         if (!dscreen->no_flush)
            pipe->flush(pipe, NULL, 0);
         dd_dump_call(dctx, call, 0);
         break;
      default:
         assert(0);
      }
   }

   ++dctx->num_draw_calls;
   if (dscreen->skip_count && dctx->num_draw_calls % 10000 == 0)
      fprintf(stderr, "Gallium debugger reached %u draw calls.\n",
              dctx->num_draw_calls);
}

static void
dd_context_draw_vbo(struct pipe_context *_pipe,
                    const struct pipe_draw_info *info)
{
   struct dd_context *dctx = dd_context(_pipe);
   struct pipe_context *pipe = dctx->pipe;
   struct dd_call call;

   call.type = CALL_DRAW_VBO;
   call.info.draw_vbo = *info;

   dd_before_draw(dctx);
   pipe->draw_vbo(pipe, info);
   dd_after_draw(dctx, &call);
}

static void
dd_context_resource_copy_region(struct pipe_context *_pipe,
                                struct pipe_resource *dst, unsigned dst_level,
                                unsigned dstx, unsigned dsty, unsigned dstz,
                                struct pipe_resource *src, unsigned src_level,
                                const struct pipe_box *src_box)
{
   struct dd_context *dctx = dd_context(_pipe);
   struct pipe_context *pipe = dctx->pipe;
   struct dd_call call;

   call.type = CALL_RESOURCE_COPY_REGION;
   call.info.resource_copy_region.dst = dst;
   call.info.resource_copy_region.dst_level = dst_level;
   call.info.resource_copy_region.dstx = dstx;
   call.info.resource_copy_region.dsty = dsty;
   call.info.resource_copy_region.dstz = dstz;
   call.info.resource_copy_region.src = src;
   call.info.resource_copy_region.src_level = src_level;
   call.info.resource_copy_region.src_box = src_box;

   dd_before_draw(dctx);
   pipe->resource_copy_region(pipe,
                              dst, dst_level, dstx, dsty, dstz,
                              src, src_level, src_box);
   dd_after_draw(dctx, &call);
}

static void
dd_context_blit(struct pipe_context *_pipe, const struct pipe_blit_info *info)
{
   struct dd_context *dctx = dd_context(_pipe);
   struct pipe_context *pipe = dctx->pipe;
   struct dd_call call;

   call.type = CALL_BLIT;
   call.info.blit = *info;

   dd_before_draw(dctx);
   pipe->blit(pipe, info);
   dd_after_draw(dctx, &call);
}

static void
dd_context_flush_resource(struct pipe_context *_pipe,
                          struct pipe_resource *resource)
{
   struct dd_context *dctx = dd_context(_pipe);
   struct pipe_context *pipe = dctx->pipe;
   struct dd_call call;

   call.type = CALL_FLUSH_RESOURCE;
   call.info.flush_resource = resource;

   dd_before_draw(dctx);
   pipe->flush_resource(pipe, resource);
   dd_after_draw(dctx, &call);
}

static void
dd_context_clear(struct pipe_context *_pipe, unsigned buffers,
                 const union pipe_color_union *color, double depth,
                 unsigned stencil)
{
   struct dd_context *dctx = dd_context(_pipe);
   struct pipe_context *pipe = dctx->pipe;
   struct dd_call call;

   call.type = CALL_CLEAR;
   call.info.clear.buffers = buffers;
   call.info.clear.color = color;
   call.info.clear.depth = depth;
   call.info.clear.stencil = stencil;

   dd_before_draw(dctx);
   pipe->clear(pipe, buffers, color, depth, stencil);
   dd_after_draw(dctx, &call);
}

static void
dd_context_clear_render_target(struct pipe_context *_pipe,
                               struct pipe_surface *dst,
                               const union pipe_color_union *color,
                               unsigned dstx, unsigned dsty,
                               unsigned width, unsigned height)
{
   struct dd_context *dctx = dd_context(_pipe);
   struct pipe_context *pipe = dctx->pipe;
   struct dd_call call;

   call.type = CALL_CLEAR_RENDER_TARGET;

   dd_before_draw(dctx);
   pipe->clear_render_target(pipe, dst, color, dstx, dsty, width, height);
   dd_after_draw(dctx, &call);
}

static void
dd_context_clear_depth_stencil(struct pipe_context *_pipe,
                               struct pipe_surface *dst, unsigned clear_flags,
                               double depth, unsigned stencil, unsigned dstx,
                               unsigned dsty, unsigned width, unsigned height)
{
   struct dd_context *dctx = dd_context(_pipe);
   struct pipe_context *pipe = dctx->pipe;
   struct dd_call call;

   call.type = CALL_CLEAR_DEPTH_STENCIL;

   dd_before_draw(dctx);
   pipe->clear_depth_stencil(pipe, dst, clear_flags, depth, stencil,
                             dstx, dsty, width, height);
   dd_after_draw(dctx, &call);
}

static void
dd_context_clear_buffer(struct pipe_context *_pipe, struct pipe_resource *res,
                        unsigned offset, unsigned size,
                        const void *clear_value, int clear_value_size)
{
   struct dd_context *dctx = dd_context(_pipe);
   struct pipe_context *pipe = dctx->pipe;
   struct dd_call call;

   call.type = CALL_CLEAR_BUFFER;
   call.info.clear_buffer.res = res;
   call.info.clear_buffer.offset = offset;
   call.info.clear_buffer.size = size;
   call.info.clear_buffer.clear_value = clear_value;
   call.info.clear_buffer.clear_value_size = clear_value_size;

   dd_before_draw(dctx);
   pipe->clear_buffer(pipe, res, offset, size, clear_value, clear_value_size);
   dd_after_draw(dctx, &call);
}

void
dd_init_draw_functions(struct dd_context *dctx)
{
   CTX_INIT(flush);
   CTX_INIT(draw_vbo);
   CTX_INIT(resource_copy_region);
   CTX_INIT(blit);
   CTX_INIT(clear);
   CTX_INIT(clear_render_target);
   CTX_INIT(clear_depth_stencil);
   CTX_INIT(clear_buffer);
   CTX_INIT(flush_resource);
   /* launch_grid */
}