gallium/hud: control visibility at startup and runtime.
[mesa.git] / src / gallium / auxiliary / hud / hud_context.c
index 983f0575687cb7ff4bd3dd2085768c97965ea859..a055480646d4c3edb3aeaa964b3aa9bf6d7819d9 100644 (file)
@@ -33,6 +33,9 @@
  * Set GALLIUM_HUD=help for more info.
  */
 
+#include <signal.h>
+#include <stdio.h>
+
 #include "hud/hud_context.h"
 #include "hud/hud_private.h"
 #include "hud/font.h"
 #include "util/u_inlines.h"
 #include "util/u_memory.h"
 #include "util/u_math.h"
+#include "util/u_sampler.h"
 #include "util/u_simple_shaders.h"
 #include "util/u_string.h"
 #include "util/u_upload_mgr.h"
 #include "tgsi/tgsi_text.h"
 #include "tgsi/tgsi_dump.h"
 
+/* Control the visibility of all HUD contexts */
+static boolean huds_visible = TRUE;
 
 struct hud_context {
    struct pipe_context *pipe;
@@ -90,12 +96,13 @@ struct hud_context {
       unsigned max_num_vertices;
       unsigned num_vertices;
    } text, bg, whitelines;
-
-   struct {
-      boolean query_pipeline_statistics;
-   } cap;
 };
 
+static void
+signal_visible_handler(int sig, siginfo_t *siginfo, void *context)
+{
+   huds_visible = !huds_visible;
+}
 
 static void
 hud_draw_colored_prims(struct hud_context *hud, unsigned prim,
@@ -110,8 +117,8 @@ hud_draw_colored_prims(struct hud_context *hud, unsigned prim,
    hud->constants.color[1] = g;
    hud->constants.color[2] = b;
    hud->constants.color[3] = a;
-   hud->constants.translate[0] = xoffset;
-   hud->constants.translate[1] = yoffset;
+   hud->constants.translate[0] = (float) xoffset;
+   hud->constants.translate[1] = (float) yoffset;
    hud->constants.scale[0] = 1;
    hud->constants.scale[1] = yscale;
    cso_set_constant_buffer(cso, PIPE_SHADER_VERTEX, 0, &hud->constbuf);
@@ -131,10 +138,10 @@ hud_draw_colored_quad(struct hud_context *hud, unsigned prim,
                       float r, float g, float b, float a)
 {
    float buffer[] = {
-      x1, y1,
-      x1, y2,
-      x2, y2,
-      x2, y1,
+      (float) x1, (float) y1,
+      (float) x1, (float) y2,
+      (float) x2, (float) y2,
+      (float) x2, (float) y1,
    };
 
    hud_draw_colored_prims(hud, prim, buffer, 4, r, g, b, a, 0, 0, 1);
@@ -149,17 +156,17 @@ hud_draw_background_quad(struct hud_context *hud,
 
    assert(hud->bg.num_vertices + 4 <= hud->bg.max_num_vertices);
 
-   vertices[num++] = x1;
-   vertices[num++] = y1;
+   vertices[num++] = (float) x1;
+   vertices[num++] = (float) y1;
 
-   vertices[num++] = x1;
-   vertices[num++] = y2;
+   vertices[num++] = (float) x1;
+   vertices[num++] = (float) y2;
 
-   vertices[num++] = x2;
-   vertices[num++] = y2;
+   vertices[num++] = (float) x2;
+   vertices[num++] = (float) y2;
 
-   vertices[num++] = x2;
-   vertices[num++] = y1;
+   vertices[num++] = (float) x2;
+   vertices[num++] = (float) y1;
 
    hud->bg.num_vertices += num/2;
 }
@@ -204,25 +211,25 @@ hud_draw_string(struct hud_context *hud, unsigned x, unsigned y,
 
       assert(hud->text.num_vertices + num/4 + 4 <= hud->text.max_num_vertices);
 
-      vertices[num++] = x1;
-      vertices[num++] = y1;
-      vertices[num++] = tx1;
-      vertices[num++] = ty1;
+      vertices[num++] = (float) x1;
+      vertices[num++] = (float) y1;
+      vertices[num++] = (float) tx1;
+      vertices[num++] = (float) ty1;
 
-      vertices[num++] = x1;
-      vertices[num++] = y2;
-      vertices[num++] = tx1;
-      vertices[num++] = ty2;
+      vertices[num++] = (float) x1;
+      vertices[num++] = (float) y2;
+      vertices[num++] = (float) tx1;
+      vertices[num++] = (float) ty2;
 
-      vertices[num++] = x2;
-      vertices[num++] = y2;
-      vertices[num++] = tx2;
-      vertices[num++] = ty2;
+      vertices[num++] = (float) x2;
+      vertices[num++] = (float) y2;
+      vertices[num++] = (float) tx2;
+      vertices[num++] = (float) ty2;
 
-      vertices[num++] = x2;
-      vertices[num++] = y1;
-      vertices[num++] = tx2;
-      vertices[num++] = ty1;
+      vertices[num++] = (float) x2;
+      vertices[num++] = (float) y1;
+      vertices[num++] = (float) tx2;
+      vertices[num++] = (float) ty1;
 
       x += hud->font.glyph_width;
       s++;
@@ -232,18 +239,53 @@ hud_draw_string(struct hud_context *hud, unsigned x, unsigned y,
 }
 
 static void
-number_to_human_readable(uint64_t num, boolean is_in_bytes, char *out)
+number_to_human_readable(uint64_t num, uint64_t max_value,
+                         enum pipe_driver_query_type type, char *out)
 {
    static const char *byte_units[] =
-      {"", " KB", " MB", " GB", " TB", " PB", " EB"};
+      {" B", " KB", " MB", " GB", " TB", " PB", " EB"};
    static const char *metric_units[] =
       {"", " k", " M", " G", " T", " P", " E"};
-   const char **units = is_in_bytes ? byte_units : metric_units;
-   double divisor = is_in_bytes ? 1024 : 1000;
-   int unit = 0;
+   static const char *time_units[] =
+      {" us", " ms", " s"};  /* based on microseconds */
+   static const char *hz_units[] =
+      {" Hz", " KHz", " MHz", " GHz"};
+   static const char *percent_units[] = {"%"};
+
+   const char **units;
+   unsigned max_unit;
+   double divisor = (type == PIPE_DRIVER_QUERY_TYPE_BYTES) ? 1024 : 1000;
+   unsigned unit = 0;
    double d = num;
 
-   while (d > divisor) {
+   switch (type) {
+   case PIPE_DRIVER_QUERY_TYPE_MICROSECONDS:
+      max_unit = ARRAY_SIZE(time_units)-1;
+      units = time_units;
+      break;
+   case PIPE_DRIVER_QUERY_TYPE_PERCENTAGE:
+      max_unit = ARRAY_SIZE(percent_units)-1;
+      units = percent_units;
+      break;
+   case PIPE_DRIVER_QUERY_TYPE_BYTES:
+      max_unit = ARRAY_SIZE(byte_units)-1;
+      units = byte_units;
+      break;
+   case PIPE_DRIVER_QUERY_TYPE_HZ:
+      max_unit = ARRAY_SIZE(hz_units)-1;
+      units = hz_units;
+      break;
+   default:
+      if (max_value == 100) {
+         max_unit = ARRAY_SIZE(percent_units)-1;
+         units = percent_units;
+      } else {
+         max_unit = ARRAY_SIZE(metric_units)-1;
+         units = metric_units;
+      }
+   }
+
+   while (d > divisor && unit < max_unit) {
       d /= divisor;
       unit++;
    }
@@ -301,9 +343,9 @@ hud_pane_accumulate_vertices(struct hud_context *hud,
       unsigned y = pane->inner_y1 + pane->inner_height * (5 - i) / 5 -
                    hud->font.glyph_height / 2;
 
-      number_to_human_readable(pane->max_value * i / 5,
-                               pane->uses_byte_units, str);
-      hud_draw_string(hud, x, y, str);
+      number_to_human_readable(pane->max_value * i / 5, pane->max_value,
+                               pane->type, str);
+      hud_draw_string(hud, x, y, "%s", str);
    }
 
    /* draw info below the pane */
@@ -312,33 +354,33 @@ hud_pane_accumulate_vertices(struct hud_context *hud,
       unsigned x = pane->x1 + 2;
       unsigned y = pane->y2 + 2 + i*hud->font.glyph_height;
 
-      number_to_human_readable(gr->current_value,
-                               pane->uses_byte_units, str);
+      number_to_human_readable(gr->current_value, pane->max_value,
+                               pane->type, str);
       hud_draw_string(hud, x, y, "  %s: %s", gr->name, str);
       i++;
    }
 
    /* draw border */
    assert(hud->whitelines.num_vertices + num/2 + 8 <= hud->whitelines.max_num_vertices);
-   line_verts[num++] = pane->x1;
-   line_verts[num++] = pane->y1;
-   line_verts[num++] = pane->x2;
-   line_verts[num++] = pane->y1;
-
-   line_verts[num++] = pane->x2;
-   line_verts[num++] = pane->y1;
-   line_verts[num++] = pane->x2;
-   line_verts[num++] = pane->y2;
-
-   line_verts[num++] = pane->x1;
-   line_verts[num++] = pane->y2;
-   line_verts[num++] = pane->x2;
-   line_verts[num++] = pane->y2;
-
-   line_verts[num++] = pane->x1;
-   line_verts[num++] = pane->y1;
-   line_verts[num++] = pane->x1;
-   line_verts[num++] = pane->y2;
+   line_verts[num++] = (float) pane->x1;
+   line_verts[num++] = (float) pane->y1;
+   line_verts[num++] = (float) pane->x2;
+   line_verts[num++] = (float) pane->y1;
+
+   line_verts[num++] = (float) pane->x2;
+   line_verts[num++] = (float) pane->y1;
+   line_verts[num++] = (float) pane->x2;
+   line_verts[num++] = (float) pane->y2;
+
+   line_verts[num++] = (float) pane->x1;
+   line_verts[num++] = (float) pane->y2;
+   line_verts[num++] = (float) pane->x2;
+   line_verts[num++] = (float) pane->y2;
+
+   line_verts[num++] = (float) pane->x1;
+   line_verts[num++] = (float) pane->y1;
+   line_verts[num++] = (float) pane->x1;
+   line_verts[num++] = (float) pane->y2;
 
    /* draw horizontal lines inside the graph */
    for (i = 0; i <= 5; i++) {
@@ -407,22 +449,28 @@ hud_draw(struct hud_context *hud, struct pipe_resource *tex)
    struct hud_pane *pane;
    struct hud_graph *gr;
 
+   if (!huds_visible)
+      return;
+
    hud->fb_width = tex->width0;
    hud->fb_height = tex->height0;
-   hud->constants.two_div_fb_width = 2.0 / hud->fb_width;
-   hud->constants.two_div_fb_height = 2.0 / hud->fb_height;
+   hud->constants.two_div_fb_width = 2.0f / hud->fb_width;
+   hud->constants.two_div_fb_height = 2.0f / hud->fb_height;
 
    cso_save_framebuffer(cso);
    cso_save_sample_mask(cso);
+   cso_save_min_samples(cso);
    cso_save_blend(cso);
    cso_save_depth_stencil_alpha(cso);
    cso_save_fragment_shader(cso);
-   cso_save_sampler_views(cso, PIPE_SHADER_FRAGMENT);
-   cso_save_samplers(cso, PIPE_SHADER_FRAGMENT);
+   cso_save_fragment_sampler_views(cso);
+   cso_save_fragment_samplers(cso);
    cso_save_rasterizer(cso);
    cso_save_viewport(cso);
    cso_save_stream_outputs(cso);
    cso_save_geometry_shader(cso);
+   cso_save_tessctrl_shader(cso);
+   cso_save_tesseval_shader(cso);
    cso_save_vertex_shader(cso);
    cso_save_vertex_elements(cso);
    cso_save_aux_vertex_buffer_slot(cso);
@@ -444,30 +492,31 @@ hud_draw(struct hud_context *hud, struct pipe_resource *tex)
    viewport.scale[0] = 0.5f * hud->fb_width;
    viewport.scale[1] = 0.5f * hud->fb_height;
    viewport.scale[2] = 1.0f;
-   viewport.scale[3] = 1.0f;
    viewport.translate[0] = 0.5f * hud->fb_width;
    viewport.translate[1] = 0.5f * hud->fb_height;
    viewport.translate[2] = 0.0f;
-   viewport.translate[3] = 0.0f;
 
    cso_set_framebuffer(cso, &fb);
    cso_set_sample_mask(cso, ~0);
+   cso_set_min_samples(cso, 1);
    cso_set_blend(cso, &hud->alpha_blend);
    cso_set_depth_stencil_alpha(cso, &hud->dsa);
    cso_set_rasterizer(cso, &hud->rasterizer);
    cso_set_viewport(cso, &viewport);
-   cso_set_stream_outputs(cso, 0, NULL, 0);
+   cso_set_stream_outputs(cso, 0, NULL, NULL);
+   cso_set_tessctrl_shader_handle(cso, NULL);
+   cso_set_tesseval_shader_handle(cso, NULL);
    cso_set_geometry_shader_handle(cso, NULL);
    cso_set_vertex_shader_handle(cso, hud->vs);
    cso_set_vertex_elements(cso, 2, hud->velems);
-   cso_set_render_condition(cso, NULL, 0);
+   cso_set_render_condition(cso, NULL, FALSE, 0);
    cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1,
                          &hud->font_sampler_view);
    cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, sampler_states);
    cso_set_constant_buffer(cso, PIPE_SHADER_VERTEX, 0, &hud->constbuf);
 
    /* prepare vertex buffers */
-   hud_alloc_vertices(hud, &hud->bg, 4 * 64, 2 * sizeof(float));
+   hud_alloc_vertices(hud, &hud->bg, 4 * 128, 2 * sizeof(float));
    hud_alloc_vertices(hud, &hud->whitelines, 4 * 256, 2 * sizeof(float));
    hud_alloc_vertices(hud, &hud->text, 4 * 512, 4 * sizeof(float));
 
@@ -481,7 +530,7 @@ hud_draw(struct hud_context *hud, struct pipe_resource *tex)
    }
 
    /* unmap the uploader's vertex buffer before drawing */
-   u_upload_flush(hud->uploader);
+   u_upload_unmap(hud->uploader);
 
    /* draw accumulated vertices for background quads */
    cso_set_fragment_shader_handle(hud->cso, hud->fs_color);
@@ -490,7 +539,7 @@ hud_draw(struct hud_context *hud, struct pipe_resource *tex)
       hud->constants.color[0] = 0;
       hud->constants.color[1] = 0;
       hud->constants.color[2] = 0;
-      hud->constants.color[3] = 0.666;
+      hud->constants.color[3] = 0.666f;
       hud->constants.translate[0] = 0;
       hud->constants.translate[1] = 0;
       hud->constants.scale[0] = 1;
@@ -540,14 +589,17 @@ hud_draw(struct hud_context *hud, struct pipe_resource *tex)
    /* restore states */
    cso_restore_framebuffer(cso);
    cso_restore_sample_mask(cso);
+   cso_restore_min_samples(cso);
    cso_restore_blend(cso);
    cso_restore_depth_stencil_alpha(cso);
    cso_restore_fragment_shader(cso);
-   cso_restore_sampler_views(cso, PIPE_SHADER_FRAGMENT);
-   cso_restore_samplers(cso, PIPE_SHADER_FRAGMENT);
+   cso_restore_fragment_sampler_views(cso);
+   cso_restore_fragment_samplers(cso);
    cso_restore_rasterizer(cso);
    cso_restore_viewport(cso);
    cso_restore_stream_outputs(cso);
+   cso_restore_tessctrl_shader(cso);
+   cso_restore_tesseval_shader(cso);
    cso_restore_geometry_shader(cso);
    cso_restore_vertex_shader(cso);
    cso_restore_vertex_elements(cso);
@@ -566,12 +618,39 @@ void
 hud_pane_set_max_value(struct hud_pane *pane, uint64_t value)
 {
    pane->max_value = value;
-   pane->yscale = -(int)pane->inner_height / (double)pane->max_value;
+   pane->yscale = -(int)pane->inner_height / (float)pane->max_value;
+}
+
+static void
+hud_pane_update_dyn_ceiling(struct hud_graph *gr, struct hud_pane *pane)
+{
+   unsigned i;
+   float tmp = 0.0f;
+
+   if (pane->dyn_ceil_last_ran != gr->index) {
+      LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
+         for (i = 0; i <  gr->num_vertices; ++i) {
+            tmp = gr->vertices[i * 2 + 1] > tmp ?
+                  gr->vertices[i * 2 + 1] : tmp;
+         }
+      }
+
+      /* Avoid setting it lower than the initial starting height. */
+      tmp = tmp > pane->initial_max_value ? tmp : pane->initial_max_value;
+      hud_pane_set_max_value(pane, tmp);
+   }
+
+   /*
+    * Mark this adjustment run so we could avoid repeating a full update
+    * again needlessly in case the pane has more than one graph.
+    */
+   pane->dyn_ceil_last_ran = gr->index;
 }
 
 static struct hud_pane *
 hud_pane_create(unsigned x1, unsigned y1, unsigned x2, unsigned y2,
-                unsigned period, uint64_t max_value)
+                unsigned period, uint64_t max_value, uint64_t ceiling,
+                boolean dyn_ceiling)
 {
    struct hud_pane *pane = CALLOC_STRUCT(hud_pane);
 
@@ -590,6 +669,10 @@ hud_pane_create(unsigned x1, unsigned y1, unsigned x2, unsigned y2,
    pane->inner_height = pane->inner_y2 - pane->inner_y1;
    pane->period = period;
    pane->max_num_vertices = (x2 - x1 + 2) / 2;
+   pane->ceiling = ceiling;
+   pane->dyn_ceiling = dyn_ceiling;
+   pane->dyn_ceil_last_ran = 0;
+   pane->initial_max_value = max_value;
    hud_pane_set_max_value(pane, max_value);
    LIST_INITHEAD(&pane->graph_list);
    return pane;
@@ -633,20 +716,25 @@ hud_pane_add_graph(struct hud_pane *pane, struct hud_graph *gr)
 void
 hud_graph_add_value(struct hud_graph *gr, uint64_t value)
 {
+   gr->current_value = value;
+   value = value > gr->pane->ceiling ? gr->pane->ceiling : value;
+
    if (gr->index == gr->pane->max_num_vertices) {
       gr->vertices[0] = 0;
       gr->vertices[1] = gr->vertices[(gr->index-1)*2+1];
       gr->index = 1;
    }
-   gr->vertices[(gr->index)*2+0] = gr->index*2;
-   gr->vertices[(gr->index)*2+1] = value;
+   gr->vertices[(gr->index)*2+0] = (float) (gr->index * 2);
+   gr->vertices[(gr->index)*2+1] = (float) value;
    gr->index++;
 
    if (gr->num_vertices < gr->pane->max_num_vertices) {
       gr->num_vertices++;
    }
 
-   gr->current_value = value;
+   if (gr->pane->dyn_ceiling == true) {
+      hud_pane_update_dyn_ceiling(gr, gr->pane);
+   }
    if (value > gr->pane->max_value) {
       hud_pane_set_max_value(gr->pane, value);
    }
@@ -683,6 +771,69 @@ parse_string(const char *s, char *out)
    return i;
 }
 
+static char *
+read_pane_settings(char *str, unsigned * const x, unsigned * const y,
+               unsigned * const width, unsigned * const height,
+               uint64_t * const ceiling, boolean * const dyn_ceiling)
+{
+   char *ret = str;
+   unsigned tmp;
+
+   while (*str == '.') {
+      ++str;
+      switch (*str) {
+      case 'x':
+         ++str;
+         *x = strtoul(str, &ret, 10);
+         str = ret;
+         break;
+
+      case 'y':
+         ++str;
+         *y = strtoul(str, &ret, 10);
+         str = ret;
+         break;
+
+      case 'w':
+         ++str;
+         tmp = strtoul(str, &ret, 10);
+         *width = tmp > 80 ? tmp : 80; /* 80 is chosen arbitrarily */
+         str = ret;
+         break;
+
+      /*
+       * Prevent setting height to less than 50. If the height is set to less,
+       * the text of the Y axis labels on the graph will start overlapping.
+       */
+      case 'h':
+         ++str;
+         tmp = strtoul(str, &ret, 10);
+         *height = tmp > 50 ? tmp : 50;
+         str = ret;
+         break;
+
+      case 'c':
+         ++str;
+         tmp = strtoul(str, &ret, 10);
+         *ceiling = tmp > 10 ? tmp : 10;
+         str = ret;
+         break;
+
+      case 'd':
+         ++str;
+         ret = str;
+         *dyn_ceiling = true;
+         break;
+
+      default:
+         fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *str);
+      }
+
+   }
+
+   return ret;
+}
+
 static boolean
 has_occlusion_query(struct pipe_screen *screen)
 {
@@ -695,15 +846,25 @@ has_streamout(struct pipe_screen *screen)
    return screen->get_param(screen, PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
 }
 
+static boolean
+has_pipeline_stats_query(struct pipe_screen *screen)
+{
+   return screen->get_param(screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS) != 0;
+}
+
 static void
 hud_parse_env_var(struct hud_context *hud, const char *env)
 {
    unsigned num, i;
-   char name[256], s[256];
+   char name_a[256], s[256];
+   char *name;
    struct hud_pane *pane = NULL;
    unsigned x = 10, y = 10;
    unsigned width = 251, height = 100;
    unsigned period = 500 * 1000;  /* default period (1/2 second) */
+   uint64_t ceiling = UINT64_MAX;
+   unsigned column_width = 251;
+   boolean dyn_ceiling = false;
    const char *period_env;
 
    /*
@@ -713,22 +874,35 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
     */
    period_env = getenv("GALLIUM_HUD_PERIOD");
    if (period_env) {
-      float p = atof(period_env);
-      if (p >= 0.0) {
+      float p = (float) atof(period_env);
+      if (p >= 0.0f) {
          period = (unsigned) (p * 1000 * 1000);
       }
    }
 
-   while ((num = parse_string(env, name)) != 0) {
+   while ((num = parse_string(env, name_a)) != 0) {
       env += num;
 
+      /* check for explicit location, size and etc. settings */
+      name = read_pane_settings(name_a, &x, &y, &width, &height, &ceiling,
+                         &dyn_ceiling);
+
+     /*
+      * Keep track of overall column width to avoid pane overlapping in case
+      * later we create a new column while the bottom pane in the current
+      * column is less wide than the rest of the panes in it.
+      */
+     column_width = width > column_width ? width : column_width;
+
       if (!pane) {
-         pane = hud_pane_create(x, y, x + width, y + height, period, 10);
+         pane = hud_pane_create(x, y, x + width, y + height, period, 10,
+                         ceiling, dyn_ceiling);
          if (!pane)
             return;
       }
 
-      /* add a graph */
+      /* Add a graph. */
+      /* IF YOU CHANGE THIS, UPDATE print_help! */
       if (strcmp(name, "fps") == 0) {
          hud_fps_graph_install(pane);
       }
@@ -741,15 +915,22 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
       else if (strcmp(name, "samples-passed") == 0 &&
                has_occlusion_query(hud->pipe->screen)) {
          hud_pipe_query_install(pane, hud->pipe, "samples-passed",
-                                PIPE_QUERY_OCCLUSION_COUNTER, 0, 0, FALSE);
+                                PIPE_QUERY_OCCLUSION_COUNTER, 0, 0,
+                                PIPE_DRIVER_QUERY_TYPE_UINT64,
+                                PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE);
       }
       else if (strcmp(name, "primitives-generated") == 0 &&
                has_streamout(hud->pipe->screen)) {
          hud_pipe_query_install(pane, hud->pipe, "primitives-generated",
-                                PIPE_QUERY_PRIMITIVES_GENERATED, 0, 0, FALSE);
+                                PIPE_QUERY_PRIMITIVES_GENERATED, 0, 0,
+                                PIPE_DRIVER_QUERY_TYPE_UINT64,
+                                PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE);
       }
-      else if (strncmp(name, "pipeline-statistics-", 20) == 0) {
-         if (hud->cap.query_pipeline_statistics) {
+      else {
+         boolean processed = FALSE;
+
+         /* pipeline statistics queries */
+         if (has_pipeline_stats_query(hud->pipe->screen)) {
             static const char *pipeline_statistics_names[] =
             {
                "ia-vertices",
@@ -765,22 +946,22 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
                "cs-invocations"
             };
             for (i = 0; i < Elements(pipeline_statistics_names); ++i)
-               if (strcmp(&name[20], pipeline_statistics_names[i]) == 0)
+               if (strcmp(name, pipeline_statistics_names[i]) == 0)
                   break;
-            if (i < Elements(pipeline_statistics_names))
-               hud_pipe_query_install(pane, hud->pipe, &name[20],
+            if (i < Elements(pipeline_statistics_names)) {
+               hud_pipe_query_install(pane, hud->pipe, name,
                                       PIPE_QUERY_PIPELINE_STATISTICS, i,
-                                      0, FALSE);
-            else
-               fprintf(stderr, "gallium_hud: invalid pipeline-statistics-*\n");
-         } else {
-            fprintf(stderr, "gallium_hud: PIPE_QUERY_PIPELINE_STATISTICS "
-                    "not supported by the driver\n");
+                                      0, PIPE_DRIVER_QUERY_TYPE_UINT64,
+                                      PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE);
+               processed = TRUE;
+            }
          }
-      }
-      else {
-         if (!hud_driver_query_install(pane, hud->pipe, name)){
-            fprintf(stderr, "gallium_hud: unknown driver query '%s'\n", name);
+
+         /* driver queries */
+         if (!processed) {
+            if (!hud_driver_query_install(pane, hud->pipe, name)){
+               fprintf(stderr, "gallium_hud: unknown driver query '%s'\n", name);
+            }
          }
       }
 
@@ -798,6 +979,7 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
 
          if (num && sscanf(s, "%u", &i) == 1) {
             hud_pane_set_max_value(pane, i);
+            pane->initial_max_value = i;
          }
          else {
             fprintf(stderr, "gallium_hud: syntax error: unexpected '%c' (%i) "
@@ -816,7 +998,11 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
 
       case ',':
          env++;
+         if (!pane)
+            break;
+
          y += height + hud->font.glyph_height * (pane->num_graphs + 2);
+         height = 100;
 
          if (pane && pane->num_graphs) {
             LIST_ADDTAIL(&pane->head, &hud->pane_list);
@@ -827,17 +1013,27 @@ hud_parse_env_var(struct hud_context *hud, const char *env)
       case ';':
          env++;
          y = 10;
-         x += width + hud->font.glyph_width * 7;
+         x += column_width + hud->font.glyph_width * 7;
+         height = 100;
 
          if (pane && pane->num_graphs) {
             LIST_ADDTAIL(&pane->head, &hud->pane_list);
             pane = NULL;
          }
+
+         /* Starting a new column; reset column width. */
+         column_width = 251;
          break;
 
       default:
          fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *env);
       }
+
+      /* Reset to defaults for the next pane in case these were modified. */
+      width = 251;
+      ceiling = UINT64_MAX;
+      dyn_ceiling = false;
+
    }
 
    if (pane) {
@@ -869,6 +1065,30 @@ print_help(struct pipe_screen *screen)
    puts("");
    puts("  Example: GALLIUM_HUD=\"cpu,fps;primitives-generated\"");
    puts("");
+   puts("  Additionally, by prepending '.[identifier][value]' modifiers to");
+   puts("  a name, it is possible to explicitly set the location and size");
+   puts("  of a pane, along with limiting overall maximum value of the");
+   puts("  Y axis and activating dynamic readjustment of the Y axis.");
+   puts("  Several modifiers may be applied to the same pane simultaneously.");
+   puts("");
+   puts("  'x[value]' sets the location of the pane on the x axis relative");
+   puts("             to the upper-left corner of the viewport, in pixels.");
+   puts("  'y[value]' sets the location of the pane on the y axis relative");
+   puts("             to the upper-left corner of the viewport, in pixels.");
+   puts("  'w[value]' sets width of the graph pixels.");
+   puts("  'h[value]' sets height of the graph in pixels.");
+   puts("  'c[value]' sets the ceiling of the value of the Y axis.");
+   puts("             If the graph needs to draw values higher than");
+   puts("             the ceiling allows, the value is clamped.");
+   puts("  'd' activates dynamic Y axis readjustment to set the value of");
+   puts("      the Y axis to match the highest value still visible in the graph.");
+   puts("");
+   puts("  If 'c' and 'd' modifiers are used simultaneously, both are in effect:");
+   puts("  the Y axis does not go above the restriction imposed by 'c' while");
+   puts("  still adjusting the value of the Y axis down when appropriate.");
+   puts("");
+   puts("  Example: GALLIUM_HUD=\".w256.h64.x1600.y520.d.c1000fps+cpu,.datom-count\"");
+   puts("");
    puts("  Available names:");
    puts("    fps");
    puts("    cpu");
@@ -877,10 +1097,24 @@ print_help(struct pipe_screen *screen)
       printf("    cpu%i\n", i);
 
    if (has_occlusion_query(screen))
-      puts("    pixels-rendered");
+      puts("    samples-passed");
    if (has_streamout(screen))
       puts("    primitives-generated");
 
+   if (has_pipeline_stats_query(screen)) {
+      puts("    ia-vertices");
+      puts("    ia-primitives");
+      puts("    vs-invocations");
+      puts("    gs-invocations");
+      puts("    gs-primitives");
+      puts("    clipper-invocations");
+      puts("    clipper-primitives-generated");
+      puts("    ps-invocations");
+      puts("    hs-invocations");
+      puts("    ds-invocations");
+      puts("    cs-invocations");
+   }
+
    if (screen->get_driver_query_info){
       struct pipe_driver_query_info info;
       num_queries = screen->get_driver_query_info(screen, 0, NULL);
@@ -892,6 +1126,7 @@ print_help(struct pipe_screen *screen)
    }
 
    puts("");
+   fflush(stdout);
 }
 
 struct hud_context *
@@ -901,6 +1136,10 @@ hud_create(struct pipe_context *pipe, struct cso_context *cso)
    struct pipe_sampler_view view_templ;
    unsigned i;
    const char *env = debug_get_option("GALLIUM_HUD", NULL);
+   unsigned signo = debug_get_num_option("GALLIUM_HUD_TOGGLE_SIGNAL", 0);
+   static boolean sig_handled = FALSE;
+   struct sigaction action = {};
+   huds_visible = debug_get_bool_option("GALLIUM_HUD_VISIBLE", TRUE);
 
    if (!env || !*env)
       return NULL;
@@ -940,7 +1179,8 @@ hud_create(struct pipe_context *pipe, struct cso_context *cso)
    hud->fs_color =
          util_make_fragment_passthrough_shader(pipe,
                                                TGSI_SEMANTIC_COLOR,
-                                               TGSI_INTERPOLATE_CONSTANT);
+                                               TGSI_INTERPOLATE_CONSTANT,
+                                               TRUE);
 
    {
       /* Read a texture and do .xxxx swizzling. */
@@ -971,7 +1211,8 @@ hud_create(struct pipe_context *pipe, struct cso_context *cso)
    }
 
    /* rasterizer */
-   hud->rasterizer.gl_rasterization_rules = 1;
+   hud->rasterizer.half_pixel_center = 1;
+   hud->rasterizer.bottom_edge_rule = 1;
    hud->rasterizer.depth_clip = 1;
    hud->rasterizer.line_width = 1;
    hud->rasterizer.line_last_pixel = 1;
@@ -1024,12 +1265,8 @@ hud_create(struct pipe_context *pipe, struct cso_context *cso)
    }
 
    /* sampler view */
-   memset(&view_templ, 0, sizeof(view_templ));
-   view_templ.format = hud->font.texture->format;
-   view_templ.swizzle_r = PIPE_SWIZZLE_RED;
-   view_templ.swizzle_g = PIPE_SWIZZLE_GREEN;
-   view_templ.swizzle_b = PIPE_SWIZZLE_BLUE;
-   view_templ.swizzle_a = PIPE_SWIZZLE_ALPHA;
+   u_sampler_view_default_template(
+         &view_templ, hud->font.texture, hud->font.texture->format);
    hud->font_sampler_view = pipe->create_sampler_view(pipe, hud->font.texture,
                                                       &view_templ);
 
@@ -1045,8 +1282,19 @@ hud_create(struct pipe_context *pipe, struct cso_context *cso)
 
    LIST_INITHEAD(&hud->pane_list);
 
-   hud->cap.query_pipeline_statistics =
-      pipe->screen->get_param(pipe->screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS);
+   /* setup sig handler once for all hud contexts */
+   if (!sig_handled && signo != 0) {
+      action.sa_sigaction = &signal_visible_handler;
+      action.sa_flags = SA_SIGINFO;
+
+      if (signo >= NSIG)
+         fprintf(stderr, "gallium_hud: invalid signal %u\n", signo);
+      else if (sigaction(signo, &action, NULL) < 0)
+         fprintf(stderr, "gallium_hud: unable to set handler for signal %u\n", signo);
+      fflush(stderr);
+
+      sig_handled = TRUE;
+   }
 
    hud_parse_env_var(hud, env);
    return hud;