1 /**************************************************************************
3 * Copyright 2013 Marek Olšák <maraeo@gmail.com>
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 THE AUTHORS 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.
26 **************************************************************************/
28 /* This head-up display module can draw transparent graphs on top of what
29 * the app is rendering, visualizing various data like framerate, cpu load,
30 * performance counters, etc. It can be hook up into any state tracker.
32 * The HUD is controlled with the GALLIUM_HUD environment variable.
33 * Set GALLIUM_HUD=help for more info.
40 #include "hud/hud_context.h"
41 #include "hud/hud_private.h"
43 #include "cso_cache/cso_context.h"
44 #include "util/u_draw_quad.h"
45 #include "util/u_format.h"
46 #include "util/u_inlines.h"
47 #include "util/u_memory.h"
48 #include "util/u_math.h"
49 #include "util/u_sampler.h"
50 #include "util/u_simple_shaders.h"
51 #include "util/u_string.h"
52 #include "util/u_upload_mgr.h"
53 #include "tgsi/tgsi_text.h"
54 #include "tgsi/tgsi_dump.h"
56 /* Control the visibility of all HUD contexts */
57 static boolean huds_visible
= TRUE
;
62 signal_visible_handler(int sig
, siginfo_t
*siginfo
, void *context
)
64 huds_visible
= !huds_visible
;
69 hud_draw_colored_prims(struct hud_context
*hud
, unsigned prim
,
70 float *buffer
, unsigned num_vertices
,
71 float r
, float g
, float b
, float a
,
72 int xoffset
, int yoffset
, float yscale
)
74 struct cso_context
*cso
= hud
->cso
;
75 unsigned size
= num_vertices
* hud
->color_prims
.vbuf
.stride
;
77 /* If a recording context is inactive, don't draw anything. */
78 if (size
> hud
->color_prims
.buffer_size
)
81 memcpy(hud
->color_prims
.vertices
, buffer
, size
);
83 hud
->constants
.color
[0] = r
;
84 hud
->constants
.color
[1] = g
;
85 hud
->constants
.color
[2] = b
;
86 hud
->constants
.color
[3] = a
;
87 hud
->constants
.translate
[0] = (float) xoffset
;
88 hud
->constants
.translate
[1] = (float) yoffset
;
89 hud
->constants
.scale
[0] = 1;
90 hud
->constants
.scale
[1] = yscale
;
91 cso_set_constant_buffer(cso
, PIPE_SHADER_VERTEX
, 0, &hud
->constbuf
);
93 cso_set_vertex_buffers(cso
, 0, 1, &hud
->color_prims
.vbuf
);
94 cso_set_fragment_shader_handle(hud
->cso
, hud
->fs_color
);
95 cso_draw_arrays(cso
, prim
, 0, num_vertices
);
97 hud
->color_prims
.vertices
+= size
/ sizeof(float);
98 hud
->color_prims
.vbuf
.buffer_offset
+= size
;
99 hud
->color_prims
.buffer_size
-= size
;
103 hud_draw_colored_quad(struct hud_context
*hud
, unsigned prim
,
104 unsigned x1
, unsigned y1
, unsigned x2
, unsigned y2
,
105 float r
, float g
, float b
, float a
)
108 (float) x1
, (float) y1
,
109 (float) x1
, (float) y2
,
110 (float) x2
, (float) y2
,
111 (float) x2
, (float) y1
,
114 hud_draw_colored_prims(hud
, prim
, buffer
, 4, r
, g
, b
, a
, 0, 0, 1);
118 hud_draw_background_quad(struct hud_context
*hud
,
119 unsigned x1
, unsigned y1
, unsigned x2
, unsigned y2
)
121 float *vertices
= hud
->bg
.vertices
+ hud
->bg
.num_vertices
*2;
124 assert(hud
->bg
.num_vertices
+ 4 <= hud
->bg
.max_num_vertices
);
126 vertices
[num
++] = (float) x1
;
127 vertices
[num
++] = (float) y1
;
129 vertices
[num
++] = (float) x1
;
130 vertices
[num
++] = (float) y2
;
132 vertices
[num
++] = (float) x2
;
133 vertices
[num
++] = (float) y2
;
135 vertices
[num
++] = (float) x2
;
136 vertices
[num
++] = (float) y1
;
138 hud
->bg
.num_vertices
+= num
/2;
142 hud_draw_string(struct hud_context
*hud
, unsigned x
, unsigned y
,
143 const char *str
, ...)
147 float *vertices
= hud
->text
.vertices
+ hud
->text
.num_vertices
*4;
152 vsnprintf(buf
, sizeof(buf
), str
, ap
);
158 hud_draw_background_quad(hud
,
160 x
+ strlen(buf
)*hud
->font
.glyph_width
,
161 y
+ hud
->font
.glyph_height
);
166 unsigned x2
= x
+ hud
->font
.glyph_width
;
167 unsigned y2
= y
+ hud
->font
.glyph_height
;
168 unsigned tx1
= (*s
% 16) * hud
->font
.glyph_width
;
169 unsigned ty1
= (*s
/ 16) * hud
->font
.glyph_height
;
170 unsigned tx2
= tx1
+ hud
->font
.glyph_width
;
171 unsigned ty2
= ty1
+ hud
->font
.glyph_height
;
174 x
+= hud
->font
.glyph_width
;
179 assert(hud
->text
.num_vertices
+ num
/4 + 4 <= hud
->text
.max_num_vertices
);
181 vertices
[num
++] = (float) x1
;
182 vertices
[num
++] = (float) y1
;
183 vertices
[num
++] = (float) tx1
;
184 vertices
[num
++] = (float) ty1
;
186 vertices
[num
++] = (float) x1
;
187 vertices
[num
++] = (float) y2
;
188 vertices
[num
++] = (float) tx1
;
189 vertices
[num
++] = (float) ty2
;
191 vertices
[num
++] = (float) x2
;
192 vertices
[num
++] = (float) y2
;
193 vertices
[num
++] = (float) tx2
;
194 vertices
[num
++] = (float) ty2
;
196 vertices
[num
++] = (float) x2
;
197 vertices
[num
++] = (float) y1
;
198 vertices
[num
++] = (float) tx2
;
199 vertices
[num
++] = (float) ty1
;
201 x
+= hud
->font
.glyph_width
;
205 hud
->text
.num_vertices
+= num
/4;
209 number_to_human_readable(double num
, enum pipe_driver_query_type type
,
212 static const char *byte_units
[] =
213 {" B", " KB", " MB", " GB", " TB", " PB", " EB"};
214 static const char *metric_units
[] =
215 {"", " k", " M", " G", " T", " P", " E"};
216 static const char *time_units
[] =
217 {" us", " ms", " s"}; /* based on microseconds */
218 static const char *hz_units
[] =
219 {" Hz", " KHz", " MHz", " GHz"};
220 static const char *percent_units
[] = {"%"};
221 static const char *dbm_units
[] = {" (-dBm)"};
222 static const char *temperature_units
[] = {" C"};
223 static const char *volt_units
[] = {" mV", " V"};
224 static const char *amp_units
[] = {" mA", " A"};
225 static const char *watt_units
[] = {" mW", " W"};
226 static const char *float_units
[] = {""};
230 double divisor
= (type
== PIPE_DRIVER_QUERY_TYPE_BYTES
) ? 1024 : 1000;
235 case PIPE_DRIVER_QUERY_TYPE_MICROSECONDS
:
236 max_unit
= ARRAY_SIZE(time_units
)-1;
239 case PIPE_DRIVER_QUERY_TYPE_VOLTS
:
240 max_unit
= ARRAY_SIZE(volt_units
)-1;
243 case PIPE_DRIVER_QUERY_TYPE_AMPS
:
244 max_unit
= ARRAY_SIZE(amp_units
)-1;
247 case PIPE_DRIVER_QUERY_TYPE_DBM
:
248 max_unit
= ARRAY_SIZE(dbm_units
)-1;
251 case PIPE_DRIVER_QUERY_TYPE_TEMPERATURE
:
252 max_unit
= ARRAY_SIZE(temperature_units
)-1;
253 units
= temperature_units
;
255 case PIPE_DRIVER_QUERY_TYPE_FLOAT
:
256 max_unit
= ARRAY_SIZE(float_units
)-1;
259 case PIPE_DRIVER_QUERY_TYPE_PERCENTAGE
:
260 max_unit
= ARRAY_SIZE(percent_units
)-1;
261 units
= percent_units
;
263 case PIPE_DRIVER_QUERY_TYPE_BYTES
:
264 max_unit
= ARRAY_SIZE(byte_units
)-1;
267 case PIPE_DRIVER_QUERY_TYPE_HZ
:
268 max_unit
= ARRAY_SIZE(hz_units
)-1;
271 case PIPE_DRIVER_QUERY_TYPE_WATTS
:
272 max_unit
= ARRAY_SIZE(watt_units
)-1;
276 max_unit
= ARRAY_SIZE(metric_units
)-1;
277 units
= metric_units
;
280 while (d
> divisor
&& unit
< max_unit
) {
285 /* Round to 3 decimal places so as not to print trailing zeros. */
286 if (d
*1000 != (int)(d
*1000))
287 d
= round(d
* 1000) / 1000;
289 /* Show at least 4 digits with at most 3 decimal places, but not zeros. */
290 if (d
>= 1000 || d
== (int)d
)
291 sprintf(out
, "%.0f%s", d
, units
[unit
]);
292 else if (d
>= 100 || d
*10 == (int)(d
*10))
293 sprintf(out
, "%.1f%s", d
, units
[unit
]);
294 else if (d
>= 10 || d
*100 == (int)(d
*100))
295 sprintf(out
, "%.2f%s", d
, units
[unit
]);
297 sprintf(out
, "%.3f%s", d
, units
[unit
]);
301 hud_draw_graph_line_strip(struct hud_context
*hud
, const struct hud_graph
*gr
,
302 unsigned xoffset
, unsigned yoffset
, float yscale
)
304 if (gr
->num_vertices
<= 1)
307 assert(gr
->index
<= gr
->num_vertices
);
309 hud_draw_colored_prims(hud
, PIPE_PRIM_LINE_STRIP
,
310 gr
->vertices
, gr
->index
,
311 gr
->color
[0], gr
->color
[1], gr
->color
[2], 1,
312 xoffset
+ (gr
->pane
->max_num_vertices
- gr
->index
- 1) * 2 - 1,
315 if (gr
->num_vertices
<= gr
->index
)
318 hud_draw_colored_prims(hud
, PIPE_PRIM_LINE_STRIP
,
319 gr
->vertices
+ gr
->index
*2,
320 gr
->num_vertices
- gr
->index
,
321 gr
->color
[0], gr
->color
[1], gr
->color
[2], 1,
322 xoffset
- gr
->index
*2 - 1, yoffset
, yscale
);
326 hud_pane_accumulate_vertices(struct hud_context
*hud
,
327 const struct hud_pane
*pane
)
329 struct hud_graph
*gr
;
330 float *line_verts
= hud
->whitelines
.vertices
+ hud
->whitelines
.num_vertices
*2;
333 const unsigned last_line
= pane
->last_line
;
335 /* draw background */
336 hud_draw_background_quad(hud
,
340 /* draw numbers on the right-hand side */
341 for (i
= 0; i
<= last_line
; i
++) {
342 unsigned x
= pane
->x2
+ 2;
343 unsigned y
= pane
->inner_y1
+
344 pane
->inner_height
* (last_line
- i
) / last_line
-
345 hud
->font
.glyph_height
/ 2;
347 number_to_human_readable(pane
->max_value
* i
/ last_line
,
349 hud_draw_string(hud
, x
, y
, "%s", str
);
352 /* draw info below the pane */
354 LIST_FOR_EACH_ENTRY(gr
, &pane
->graph_list
, head
) {
355 unsigned x
= pane
->x1
+ 2;
356 unsigned y
= pane
->y2
+ 2 + i
*hud
->font
.glyph_height
;
358 number_to_human_readable(gr
->current_value
, pane
->type
, str
);
359 hud_draw_string(hud
, x
, y
, " %s: %s", gr
->name
, str
);
364 assert(hud
->whitelines
.num_vertices
+ num
/2 + 8 <= hud
->whitelines
.max_num_vertices
);
365 line_verts
[num
++] = (float) pane
->x1
;
366 line_verts
[num
++] = (float) pane
->y1
;
367 line_verts
[num
++] = (float) pane
->x2
;
368 line_verts
[num
++] = (float) pane
->y1
;
370 line_verts
[num
++] = (float) pane
->x2
;
371 line_verts
[num
++] = (float) pane
->y1
;
372 line_verts
[num
++] = (float) pane
->x2
;
373 line_verts
[num
++] = (float) pane
->y2
;
375 line_verts
[num
++] = (float) pane
->x1
;
376 line_verts
[num
++] = (float) pane
->y2
;
377 line_verts
[num
++] = (float) pane
->x2
;
378 line_verts
[num
++] = (float) pane
->y2
;
380 line_verts
[num
++] = (float) pane
->x1
;
381 line_verts
[num
++] = (float) pane
->y1
;
382 line_verts
[num
++] = (float) pane
->x1
;
383 line_verts
[num
++] = (float) pane
->y2
;
385 /* draw horizontal lines inside the graph */
386 for (i
= 0; i
<= last_line
; i
++) {
387 float y
= round((pane
->max_value
* i
/ (double)last_line
) *
388 pane
->yscale
+ pane
->inner_y2
);
390 assert(hud
->whitelines
.num_vertices
+ num
/2 + 2 <= hud
->whitelines
.max_num_vertices
);
391 line_verts
[num
++] = pane
->x1
;
392 line_verts
[num
++] = y
;
393 line_verts
[num
++] = pane
->x2
;
394 line_verts
[num
++] = y
;
397 hud
->whitelines
.num_vertices
+= num
/2;
401 hud_pane_accumulate_vertices_simple(struct hud_context
*hud
,
402 const struct hud_pane
*pane
)
404 struct hud_graph
*gr
;
408 /* draw info below the pane */
410 LIST_FOR_EACH_ENTRY(gr
, &pane
->graph_list
, head
) {
411 unsigned x
= pane
->x1
;
412 unsigned y
= pane
->y_simple
+ i
*hud
->font
.glyph_height
;
414 number_to_human_readable(gr
->current_value
, pane
->type
, str
);
415 hud_draw_string(hud
, x
, y
, "%s: %s", gr
->name
, str
);
421 hud_pane_draw_colored_objects(struct hud_context
*hud
,
422 const struct hud_pane
*pane
)
424 struct hud_graph
*gr
;
427 /* draw colored quads below the pane */
429 LIST_FOR_EACH_ENTRY(gr
, &pane
->graph_list
, head
) {
430 unsigned x
= pane
->x1
+ 2;
431 unsigned y
= pane
->y2
+ 2 + i
*hud
->font
.glyph_height
;
433 hud_draw_colored_quad(hud
, PIPE_PRIM_QUADS
, x
+ 1, y
+ 1, x
+ 12, y
+ 13,
434 gr
->color
[0], gr
->color
[1], gr
->color
[2], 1);
438 /* draw the line strips */
439 LIST_FOR_EACH_ENTRY(gr
, &pane
->graph_list
, head
) {
440 hud_draw_graph_line_strip(hud
, gr
, pane
->inner_x1
, pane
->inner_y2
, pane
->yscale
);
445 hud_prepare_vertices(struct hud_context
*hud
, struct vertex_queue
*v
,
446 unsigned num_vertices
, unsigned stride
)
449 v
->max_num_vertices
= num_vertices
;
450 v
->vbuf
.stride
= stride
;
451 v
->buffer_size
= stride
* num_vertices
;
455 * Draw the HUD to the texture \p tex.
456 * The texture is usually the back buffer being displayed.
459 hud_draw_results(struct hud_context
*hud
, struct pipe_resource
*tex
)
461 struct cso_context
*cso
= hud
->cso
;
462 struct pipe_context
*pipe
= hud
->pipe
;
463 struct pipe_framebuffer_state fb
;
464 struct pipe_surface surf_templ
, *surf
;
465 struct pipe_viewport_state viewport
;
466 const struct pipe_sampler_state
*sampler_states
[] =
467 { &hud
->font_sampler_state
};
468 struct hud_pane
*pane
;
473 hud
->fb_width
= tex
->width0
;
474 hud
->fb_height
= tex
->height0
;
475 hud
->constants
.two_div_fb_width
= 2.0f
/ hud
->fb_width
;
476 hud
->constants
.two_div_fb_height
= 2.0f
/ hud
->fb_height
;
478 cso_save_state(cso
, (CSO_BIT_FRAMEBUFFER
|
479 CSO_BIT_SAMPLE_MASK
|
480 CSO_BIT_MIN_SAMPLES
|
482 CSO_BIT_DEPTH_STENCIL_ALPHA
|
483 CSO_BIT_FRAGMENT_SHADER
|
484 CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
485 CSO_BIT_FRAGMENT_SAMPLERS
|
488 CSO_BIT_STREAM_OUTPUTS
|
489 CSO_BIT_GEOMETRY_SHADER
|
490 CSO_BIT_TESSCTRL_SHADER
|
491 CSO_BIT_TESSEVAL_SHADER
|
492 CSO_BIT_VERTEX_SHADER
|
493 CSO_BIT_VERTEX_ELEMENTS
|
494 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
495 CSO_BIT_PAUSE_QUERIES
|
496 CSO_BIT_RENDER_CONDITION
));
497 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_VERTEX
);
500 memset(&surf_templ
, 0, sizeof(surf_templ
));
501 surf_templ
.format
= tex
->format
;
503 /* Without this, AA lines look thinner if they are between 2 pixels
504 * because the alpha is 0.5 on both pixels. (it's ugly)
506 * sRGB makes the width of all AA lines look the same.
509 enum pipe_format srgb_format
= util_format_srgb(tex
->format
);
511 if (srgb_format
!= PIPE_FORMAT_NONE
)
512 surf_templ
.format
= srgb_format
;
514 surf
= pipe
->create_surface(pipe
, tex
, &surf_templ
);
516 memset(&fb
, 0, sizeof(fb
));
520 fb
.width
= hud
->fb_width
;
521 fb
.height
= hud
->fb_height
;
523 viewport
.scale
[0] = 0.5f
* hud
->fb_width
;
524 viewport
.scale
[1] = 0.5f
* hud
->fb_height
;
525 viewport
.scale
[2] = 1.0f
;
526 viewport
.translate
[0] = 0.5f
* hud
->fb_width
;
527 viewport
.translate
[1] = 0.5f
* hud
->fb_height
;
528 viewport
.translate
[2] = 0.0f
;
530 cso_set_framebuffer(cso
, &fb
);
531 cso_set_sample_mask(cso
, ~0);
532 cso_set_min_samples(cso
, 1);
533 cso_set_depth_stencil_alpha(cso
, &hud
->dsa
);
534 cso_set_rasterizer(cso
, &hud
->rasterizer
);
535 cso_set_viewport(cso
, &viewport
);
536 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
537 cso_set_tessctrl_shader_handle(cso
, NULL
);
538 cso_set_tesseval_shader_handle(cso
, NULL
);
539 cso_set_geometry_shader_handle(cso
, NULL
);
540 cso_set_vertex_shader_handle(cso
, hud
->vs
);
541 cso_set_vertex_elements(cso
, 2, hud
->velems
);
542 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
543 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1,
544 &hud
->font_sampler_view
);
545 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, sampler_states
);
546 cso_set_constant_buffer(cso
, PIPE_SHADER_VERTEX
, 0, &hud
->constbuf
);
548 /* draw accumulated vertices for background quads */
549 cso_set_blend(cso
, &hud
->alpha_blend
);
550 cso_set_fragment_shader_handle(hud
->cso
, hud
->fs_color
);
552 if (hud
->bg
.num_vertices
) {
553 hud
->constants
.color
[0] = 0;
554 hud
->constants
.color
[1] = 0;
555 hud
->constants
.color
[2] = 0;
556 hud
->constants
.color
[3] = 0.666f
;
557 hud
->constants
.translate
[0] = 0;
558 hud
->constants
.translate
[1] = 0;
559 hud
->constants
.scale
[0] = 1;
560 hud
->constants
.scale
[1] = 1;
562 cso_set_constant_buffer(cso
, PIPE_SHADER_VERTEX
, 0, &hud
->constbuf
);
563 cso_set_vertex_buffers(cso
, 0, 1, &hud
->bg
.vbuf
);
564 cso_draw_arrays(cso
, PIPE_PRIM_QUADS
, 0, hud
->bg
.num_vertices
);
566 pipe_resource_reference(&hud
->bg
.vbuf
.buffer
.resource
, NULL
);
568 /* draw accumulated vertices for text */
569 if (hud
->text
.num_vertices
) {
570 cso_set_vertex_buffers(cso
, 0, 1, &hud
->text
.vbuf
);
571 cso_set_fragment_shader_handle(hud
->cso
, hud
->fs_text
);
572 cso_draw_arrays(cso
, PIPE_PRIM_QUADS
, 0, hud
->text
.num_vertices
);
574 pipe_resource_reference(&hud
->text
.vbuf
.buffer
.resource
, NULL
);
577 cso_restore_state(cso
);
578 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_VERTEX
);
580 pipe_surface_reference(&surf
, NULL
);
584 /* draw accumulated vertices for white lines */
585 cso_set_blend(cso
, &hud
->no_blend
);
587 hud
->constants
.color
[0] = 1;
588 hud
->constants
.color
[1] = 1;
589 hud
->constants
.color
[2] = 1;
590 hud
->constants
.color
[3] = 1;
591 hud
->constants
.translate
[0] = 0;
592 hud
->constants
.translate
[1] = 0;
593 hud
->constants
.scale
[0] = 1;
594 hud
->constants
.scale
[1] = 1;
595 cso_set_constant_buffer(cso
, PIPE_SHADER_VERTEX
, 0, &hud
->constbuf
);
597 if (hud
->whitelines
.num_vertices
) {
598 cso_set_vertex_buffers(cso
, 0, 1, &hud
->whitelines
.vbuf
);
599 cso_set_fragment_shader_handle(hud
->cso
, hud
->fs_color
);
600 cso_draw_arrays(cso
, PIPE_PRIM_LINES
, 0, hud
->whitelines
.num_vertices
);
602 pipe_resource_reference(&hud
->whitelines
.vbuf
.buffer
.resource
, NULL
);
605 cso_set_blend(cso
, &hud
->alpha_blend
);
606 cso_set_rasterizer(cso
, &hud
->rasterizer_aa_lines
);
607 LIST_FOR_EACH_ENTRY(pane
, &hud
->pane_list
, head
) {
609 hud_pane_draw_colored_objects(hud
, pane
);
612 cso_restore_state(cso
);
613 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_VERTEX
);
615 pipe_surface_reference(&surf
, NULL
);
619 hud_start_queries(struct hud_context
*hud
, struct pipe_context
*pipe
)
621 struct hud_pane
*pane
;
622 struct hud_graph
*gr
;
625 hud_batch_query_begin(hud
->batch_query
, pipe
);
627 LIST_FOR_EACH_ENTRY(pane
, &hud
->pane_list
, head
) {
628 LIST_FOR_EACH_ENTRY(gr
, &pane
->graph_list
, head
) {
630 gr
->begin_query(gr
, pipe
);
635 /* Stop queries, query results, and record vertices for charts. */
637 hud_stop_queries(struct hud_context
*hud
, struct pipe_context
*pipe
)
639 struct hud_pane
*pane
;
640 struct hud_graph
*gr
, *next
;
642 /* prepare vertex buffers */
643 hud_prepare_vertices(hud
, &hud
->bg
, 16 * 256, 2 * sizeof(float));
644 hud_prepare_vertices(hud
, &hud
->whitelines
, 4 * 256, 2 * sizeof(float));
645 hud_prepare_vertices(hud
, &hud
->text
, 16 * 1024, 4 * sizeof(float));
646 hud_prepare_vertices(hud
, &hud
->color_prims
, 32 * 1024, 2 * sizeof(float));
648 /* Allocate everything once and divide the storage into 3 portions
649 * manually, because u_upload_alloc can unmap memory from previous calls.
651 u_upload_alloc(pipe
->stream_uploader
, 0,
652 hud
->bg
.buffer_size
+
653 hud
->whitelines
.buffer_size
+
654 hud
->text
.buffer_size
+
655 hud
->color_prims
.buffer_size
,
656 16, &hud
->bg
.vbuf
.buffer_offset
, &hud
->bg
.vbuf
.buffer
.resource
,
657 (void**)&hud
->bg
.vertices
);
658 if (!hud
->bg
.vertices
)
661 pipe_resource_reference(&hud
->whitelines
.vbuf
.buffer
.resource
, hud
->bg
.vbuf
.buffer
.resource
);
662 pipe_resource_reference(&hud
->text
.vbuf
.buffer
.resource
, hud
->bg
.vbuf
.buffer
.resource
);
663 pipe_resource_reference(&hud
->color_prims
.vbuf
.buffer
.resource
, hud
->bg
.vbuf
.buffer
.resource
);
665 hud
->whitelines
.vbuf
.buffer_offset
= hud
->bg
.vbuf
.buffer_offset
+
667 hud
->whitelines
.vertices
= hud
->bg
.vertices
+
668 hud
->bg
.buffer_size
/ sizeof(float);
670 hud
->text
.vbuf
.buffer_offset
= hud
->whitelines
.vbuf
.buffer_offset
+
671 hud
->whitelines
.buffer_size
;
672 hud
->text
.vertices
= hud
->whitelines
.vertices
+
673 hud
->whitelines
.buffer_size
/ sizeof(float);
675 hud
->color_prims
.vbuf
.buffer_offset
= hud
->text
.vbuf
.buffer_offset
+
676 hud
->text
.buffer_size
;
677 hud
->color_prims
.vertices
= hud
->text
.vertices
+
678 hud
->text
.buffer_size
/ sizeof(float);
680 /* prepare all graphs */
681 hud_batch_query_update(hud
->batch_query
, pipe
);
683 LIST_FOR_EACH_ENTRY(pane
, &hud
->pane_list
, head
) {
684 LIST_FOR_EACH_ENTRY(gr
, &pane
->graph_list
, head
) {
685 gr
->query_new_value(gr
, pipe
);
688 if (pane
->sort_items
) {
689 LIST_FOR_EACH_ENTRY_SAFE(gr
, next
, &pane
->graph_list
, head
) {
690 /* ignore the last one */
691 if (&gr
->head
== pane
->graph_list
.prev
)
694 /* This is an incremental bubble sort, because we only do one pass
695 * per frame. It will eventually reach an equilibrium.
697 if (gr
->current_value
<
698 LIST_ENTRY(struct hud_graph
, next
, head
)->current_value
) {
700 list_add(&gr
->head
, &next
->head
);
706 hud_pane_accumulate_vertices_simple(hud
, pane
);
708 hud_pane_accumulate_vertices(hud
, pane
);
711 /* unmap the uploader's vertex buffer before drawing */
712 u_upload_unmap(pipe
->stream_uploader
);
716 * Record queries and draw the HUD. The "cso" parameter acts as a filter.
717 * If "cso" is not the recording context, recording is skipped.
718 * If "cso" is not the drawing context, drawing is skipped.
719 * cso == NULL ignores the filter.
722 hud_run(struct hud_context
*hud
, struct cso_context
*cso
,
723 struct pipe_resource
*tex
)
725 struct pipe_context
*pipe
= cso
? cso_get_pipe_context(cso
) : NULL
;
727 /* If "cso" is the recording or drawing context or NULL, execute
728 * the operation. Otherwise, don't do anything.
730 if (hud
->record_pipe
&& (!pipe
|| pipe
== hud
->record_pipe
))
731 hud_stop_queries(hud
, hud
->record_pipe
);
733 if (hud
->cso
&& (!cso
|| cso
== hud
->cso
))
734 hud_draw_results(hud
, tex
);
736 if (hud
->record_pipe
&& (!pipe
|| pipe
== hud
->record_pipe
))
737 hud_start_queries(hud
, hud
->record_pipe
);
741 * Record query results and assemble vertices if "pipe" is a recording but
742 * not drawing context.
745 hud_record_only(struct hud_context
*hud
, struct pipe_context
*pipe
)
749 /* If it's a drawing context, only hud_run() records query results. */
750 if (pipe
== hud
->pipe
|| pipe
!= hud
->record_pipe
)
753 hud_stop_queries(hud
, hud
->record_pipe
);
754 hud_start_queries(hud
, hud
->record_pipe
);
758 fixup_bytes(enum pipe_driver_query_type type
, int position
, uint64_t *exp10
)
760 if (type
== PIPE_DRIVER_QUERY_TYPE_BYTES
&& position
% 3 == 0)
761 *exp10
= (*exp10
/ 1000) * 1024;
765 * Set the maximum value for the Y axis of the graph.
766 * This scales the graph accordingly.
769 hud_pane_set_max_value(struct hud_pane
*pane
, uint64_t value
)
771 double leftmost_digit
;
775 /* The following code determines the max_value in the graph as well as
776 * how many describing lines are drawn. The max_value is rounded up,
777 * so that all drawn numbers are rounded for readability.
778 * We want to print multiples of a simple number instead of multiples of
779 * hard-to-read numbers like 1.753.
782 /* Find the left-most digit. Make sure exp10 * 10 and fixup_bytes doesn't
783 * overflow. (11 is safe) */
785 for (i
= 0; exp10
<= UINT64_MAX
/ 11 && exp10
* 9 < value
; i
++) {
787 fixup_bytes(pane
->type
, i
+ 1, &exp10
);
790 leftmost_digit
= DIV_ROUND_UP(value
, exp10
);
793 if (leftmost_digit
== 9) {
796 fixup_bytes(pane
->type
, i
+ 1, &exp10
);
799 switch ((unsigned)leftmost_digit
) {
801 pane
->last_line
= 5; /* lines in +1/5 increments */
804 pane
->last_line
= 8; /* lines in +1/4 increments. */
808 pane
->last_line
= leftmost_digit
* 2; /* lines in +1/2 increments */
814 pane
->last_line
= leftmost_digit
; /* lines in +1 increments */
820 /* Truncate {3,4} to {2.5, 3.5} if possible. */
821 for (i
= 3; i
<= 4; i
++) {
822 if (leftmost_digit
== i
&& value
<= (i
- 0.5) * exp10
) {
823 leftmost_digit
= i
- 0.5;
824 pane
->last_line
= leftmost_digit
* 2; /* lines in +1/2 increments. */
828 /* Truncate 2 to a multiple of 0.2 in (1, 1.6] if possible. */
829 if (leftmost_digit
== 2) {
830 for (i
= 1; i
<= 3; i
++) {
831 if (value
<= (1 + i
*0.2) * exp10
) {
832 leftmost_digit
= 1 + i
*0.2;
833 pane
->last_line
= 5 + i
; /* lines in +1/5 increments. */
839 pane
->max_value
= leftmost_digit
* exp10
;
840 pane
->yscale
= -(int)pane
->inner_height
/ (float)pane
->max_value
;
844 hud_pane_update_dyn_ceiling(struct hud_graph
*gr
, struct hud_pane
*pane
)
849 if (pane
->dyn_ceil_last_ran
!= gr
->index
) {
850 LIST_FOR_EACH_ENTRY(gr
, &pane
->graph_list
, head
) {
851 for (i
= 0; i
< gr
->num_vertices
; ++i
) {
852 tmp
= gr
->vertices
[i
* 2 + 1] > tmp
?
853 gr
->vertices
[i
* 2 + 1] : tmp
;
857 /* Avoid setting it lower than the initial starting height. */
858 tmp
= tmp
> pane
->initial_max_value
? tmp
: pane
->initial_max_value
;
859 hud_pane_set_max_value(pane
, tmp
);
863 * Mark this adjustment run so we could avoid repeating a full update
864 * again needlessly in case the pane has more than one graph.
866 pane
->dyn_ceil_last_ran
= gr
->index
;
869 static struct hud_pane
*
870 hud_pane_create(struct hud_context
*hud
,
871 unsigned x1
, unsigned y1
, unsigned x2
, unsigned y2
,
873 unsigned period
, uint64_t max_value
, uint64_t ceiling
,
874 boolean dyn_ceiling
, boolean sort_items
)
876 struct hud_pane
*pane
= CALLOC_STRUCT(hud_pane
);
886 pane
->y_simple
= y_simple
;
887 pane
->inner_x1
= x1
+ 1;
888 pane
->inner_x2
= x2
- 1;
889 pane
->inner_y1
= y1
+ 1;
890 pane
->inner_y2
= y2
- 1;
891 pane
->inner_width
= pane
->inner_x2
- pane
->inner_x1
;
892 pane
->inner_height
= pane
->inner_y2
- pane
->inner_y1
;
893 pane
->period
= period
;
894 pane
->max_num_vertices
= (x2
- x1
+ 2) / 2;
895 pane
->ceiling
= ceiling
;
896 pane
->dyn_ceiling
= dyn_ceiling
;
897 pane
->dyn_ceil_last_ran
= 0;
898 pane
->sort_items
= sort_items
;
899 pane
->initial_max_value
= max_value
;
900 hud_pane_set_max_value(pane
, max_value
);
901 list_inithead(&pane
->graph_list
);
905 /* replace '-' with a space */
907 strip_hyphens(char *s
)
917 * Add a graph to an existing pane.
918 * One pane can contain multiple graphs over each other.
921 hud_pane_add_graph(struct hud_pane
*pane
, struct hud_graph
*gr
)
923 static const float colors
[][3] = {
940 unsigned color
= pane
->next_color
% ARRAY_SIZE(colors
);
942 strip_hyphens(gr
->name
);
944 gr
->vertices
= MALLOC(pane
->max_num_vertices
* sizeof(float) * 2);
945 gr
->color
[0] = colors
[color
][0];
946 gr
->color
[1] = colors
[color
][1];
947 gr
->color
[2] = colors
[color
][2];
949 list_addtail(&gr
->head
, &pane
->graph_list
);
955 hud_graph_add_value(struct hud_graph
*gr
, double value
)
957 gr
->current_value
= value
;
958 value
= value
> gr
->pane
->ceiling
? gr
->pane
->ceiling
: value
;
961 if (fabs(value
- lround(value
)) > FLT_EPSILON
) {
962 fprintf(gr
->fd
, "%f\n", value
);
965 fprintf(gr
->fd
, "%" PRIu64
"\n", (uint64_t) lround(value
));
969 if (gr
->index
== gr
->pane
->max_num_vertices
) {
971 gr
->vertices
[1] = gr
->vertices
[(gr
->index
-1)*2+1];
974 gr
->vertices
[(gr
->index
)*2+0] = (float) (gr
->index
* 2);
975 gr
->vertices
[(gr
->index
)*2+1] = (float) value
;
978 if (gr
->num_vertices
< gr
->pane
->max_num_vertices
) {
982 if (gr
->pane
->dyn_ceiling
== true) {
983 hud_pane_update_dyn_ceiling(gr
, gr
->pane
);
985 if (value
> gr
->pane
->max_value
) {
986 hud_pane_set_max_value(gr
->pane
, value
);
991 hud_graph_destroy(struct hud_graph
*graph
, struct pipe_context
*pipe
)
993 FREE(graph
->vertices
);
994 if (graph
->free_query_data
)
995 graph
->free_query_data(graph
->query_data
, pipe
);
1001 static void strcat_without_spaces(char *dst
, const char *src
)
1015 #ifdef PIPE_OS_WINDOWS
1018 access(const char *pathname
, int mode
)
1024 #define PATH_SEP "\\"
1028 #define PATH_SEP "/"
1034 * If the GALLIUM_HUD_DUMP_DIR env var is set, we'll write the raw
1035 * HUD values to files at ${GALLIUM_HUD_DUMP_DIR}/<stat> where <stat>
1036 * is a HUD variable such as "fps", or "cpu"
1039 hud_graph_set_dump_file(struct hud_graph
*gr
)
1041 const char *hud_dump_dir
= getenv("GALLIUM_HUD_DUMP_DIR");
1043 if (hud_dump_dir
&& access(hud_dump_dir
, W_OK
) == 0) {
1044 char *dump_file
= malloc(strlen(hud_dump_dir
) + sizeof(PATH_SEP
)
1045 + sizeof(gr
->name
));
1047 strcpy(dump_file
, hud_dump_dir
);
1048 strcat(dump_file
, PATH_SEP
);
1049 strcat_without_spaces(dump_file
, gr
->name
);
1050 gr
->fd
= fopen(dump_file
, "w+");
1052 /* flush output after each line is written */
1053 setvbuf(gr
->fd
, NULL
, _IOLBF
, 0);
1061 * Read a string from the environment variable.
1062 * The separators "+", ",", ":", and ";" terminate the string.
1063 * Return the number of read characters.
1066 parse_string(const char *s
, char *out
)
1070 for (i
= 0; *s
&& *s
!= '+' && *s
!= ',' && *s
!= ':' && *s
!= ';' && *s
!= '=';
1077 fprintf(stderr
, "gallium_hud: syntax error: unexpected '%c' (%i) while "
1078 "parsing a string\n", *s
, *s
);
1086 read_pane_settings(char *str
, unsigned * const x
, unsigned * const y
,
1087 unsigned * const width
, unsigned * const height
,
1088 uint64_t * const ceiling
, boolean
* const dyn_ceiling
,
1089 boolean
*reset_colors
, boolean
*sort_items
)
1094 while (*str
== '.') {
1099 *x
= strtoul(str
, &ret
, 10);
1105 *y
= strtoul(str
, &ret
, 10);
1111 tmp
= strtoul(str
, &ret
, 10);
1112 *width
= tmp
> 80 ? tmp
: 80; /* 80 is chosen arbitrarily */
1117 * Prevent setting height to less than 50. If the height is set to less,
1118 * the text of the Y axis labels on the graph will start overlapping.
1122 tmp
= strtoul(str
, &ret
, 10);
1123 *height
= tmp
> 50 ? tmp
: 50;
1129 tmp
= strtoul(str
, &ret
, 10);
1130 *ceiling
= tmp
> 10 ? tmp
: 10;
1137 *dyn_ceiling
= true;
1143 *reset_colors
= true;
1153 fprintf(stderr
, "gallium_hud: syntax error: unexpected '%c'\n", *str
);
1163 has_occlusion_query(struct pipe_screen
*screen
)
1165 return screen
->get_param(screen
, PIPE_CAP_OCCLUSION_QUERY
) != 0;
1169 has_streamout(struct pipe_screen
*screen
)
1171 return screen
->get_param(screen
, PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
1175 has_pipeline_stats_query(struct pipe_screen
*screen
)
1177 return screen
->get_param(screen
, PIPE_CAP_QUERY_PIPELINE_STATISTICS
) != 0;
1181 hud_parse_env_var(struct hud_context
*hud
, struct pipe_screen
*screen
,
1185 char name_a
[256], s
[256];
1187 struct hud_pane
*pane
= NULL
;
1188 unsigned x
= 10, y
= 10, y_simple
= 10;
1189 unsigned width
= 251, height
= 100;
1190 unsigned period
= 500 * 1000; /* default period (1/2 second) */
1191 uint64_t ceiling
= UINT64_MAX
;
1192 unsigned column_width
= 251;
1193 boolean dyn_ceiling
= false;
1194 boolean reset_colors
= false;
1195 boolean sort_items
= false;
1196 const char *period_env
;
1198 if (strncmp(env
, "simple,", 7) == 0) {
1204 * The GALLIUM_HUD_PERIOD env var sets the graph update rate.
1205 * The env var is in seconds (a float).
1206 * Zero means update after every frame.
1208 period_env
= getenv("GALLIUM_HUD_PERIOD");
1210 float p
= (float) atof(period_env
);
1212 period
= (unsigned) (p
* 1000 * 1000);
1216 while ((num
= parse_string(env
, name_a
)) != 0) {
1221 /* check for explicit location, size and etc. settings */
1222 name
= read_pane_settings(name_a
, &x
, &y
, &width
, &height
, &ceiling
,
1223 &dyn_ceiling
, &reset_colors
, &sort_items
);
1226 * Keep track of overall column width to avoid pane overlapping in case
1227 * later we create a new column while the bottom pane in the current
1228 * column is less wide than the rest of the panes in it.
1230 column_width
= width
> column_width
? width
: column_width
;
1233 pane
= hud_pane_create(hud
, x
, y
, x
+ width
, y
+ height
, y_simple
,
1234 period
, 10, ceiling
, dyn_ceiling
, sort_items
);
1240 pane
->next_color
= 0;
1241 reset_colors
= false;
1245 #if defined(HAVE_GALLIUM_EXTRA_HUD) || defined(HAVE_LIBSENSORS)
1248 /* IF YOU CHANGE THIS, UPDATE print_help! */
1249 if (strcmp(name
, "fps") == 0) {
1250 hud_fps_graph_install(pane
);
1252 else if (strcmp(name
, "frametime") == 0) {
1253 hud_frametime_graph_install(pane
);
1255 else if (strcmp(name
, "cpu") == 0) {
1256 hud_cpu_graph_install(pane
, ALL_CPUS
);
1258 else if (sscanf(name
, "cpu%u%s", &i
, s
) == 1) {
1259 hud_cpu_graph_install(pane
, i
);
1261 else if (strcmp(name
, "API-thread-busy") == 0) {
1262 hud_thread_busy_install(pane
, name
, false);
1264 else if (strcmp(name
, "API-thread-offloaded-slots") == 0) {
1265 hud_thread_counter_install(pane
, name
, HUD_COUNTER_OFFLOADED
);
1267 else if (strcmp(name
, "API-thread-direct-slots") == 0) {
1268 hud_thread_counter_install(pane
, name
, HUD_COUNTER_DIRECT
);
1270 else if (strcmp(name
, "API-thread-num-syncs") == 0) {
1271 hud_thread_counter_install(pane
, name
, HUD_COUNTER_SYNCS
);
1273 else if (strcmp(name
, "main-thread-busy") == 0) {
1274 hud_thread_busy_install(pane
, name
, true);
1276 #ifdef HAVE_GALLIUM_EXTRA_HUD
1277 else if (sscanf(name
, "nic-rx-%s", arg_name
) == 1) {
1278 hud_nic_graph_install(pane
, arg_name
, NIC_DIRECTION_RX
);
1280 else if (sscanf(name
, "nic-tx-%s", arg_name
) == 1) {
1281 hud_nic_graph_install(pane
, arg_name
, NIC_DIRECTION_TX
);
1283 else if (sscanf(name
, "nic-rssi-%s", arg_name
) == 1) {
1284 hud_nic_graph_install(pane
, arg_name
, NIC_RSSI_DBM
);
1285 pane
->type
= PIPE_DRIVER_QUERY_TYPE_DBM
;
1287 else if (sscanf(name
, "diskstat-rd-%s", arg_name
) == 1) {
1288 hud_diskstat_graph_install(pane
, arg_name
, DISKSTAT_RD
);
1289 pane
->type
= PIPE_DRIVER_QUERY_TYPE_BYTES
;
1291 else if (sscanf(name
, "diskstat-wr-%s", arg_name
) == 1) {
1292 hud_diskstat_graph_install(pane
, arg_name
, DISKSTAT_WR
);
1293 pane
->type
= PIPE_DRIVER_QUERY_TYPE_BYTES
;
1295 else if (sscanf(name
, "cpufreq-min-cpu%u", &i
) == 1) {
1296 hud_cpufreq_graph_install(pane
, i
, CPUFREQ_MINIMUM
);
1297 pane
->type
= PIPE_DRIVER_QUERY_TYPE_HZ
;
1299 else if (sscanf(name
, "cpufreq-cur-cpu%u", &i
) == 1) {
1300 hud_cpufreq_graph_install(pane
, i
, CPUFREQ_CURRENT
);
1301 pane
->type
= PIPE_DRIVER_QUERY_TYPE_HZ
;
1303 else if (sscanf(name
, "cpufreq-max-cpu%u", &i
) == 1) {
1304 hud_cpufreq_graph_install(pane
, i
, CPUFREQ_MAXIMUM
);
1305 pane
->type
= PIPE_DRIVER_QUERY_TYPE_HZ
;
1308 #ifdef HAVE_LIBSENSORS
1309 else if (sscanf(name
, "sensors_temp_cu-%s", arg_name
) == 1) {
1310 hud_sensors_temp_graph_install(pane
, arg_name
,
1311 SENSORS_TEMP_CURRENT
);
1312 pane
->type
= PIPE_DRIVER_QUERY_TYPE_TEMPERATURE
;
1314 else if (sscanf(name
, "sensors_temp_cr-%s", arg_name
) == 1) {
1315 hud_sensors_temp_graph_install(pane
, arg_name
,
1316 SENSORS_TEMP_CRITICAL
);
1317 pane
->type
= PIPE_DRIVER_QUERY_TYPE_TEMPERATURE
;
1319 else if (sscanf(name
, "sensors_volt_cu-%s", arg_name
) == 1) {
1320 hud_sensors_temp_graph_install(pane
, arg_name
,
1321 SENSORS_VOLTAGE_CURRENT
);
1322 pane
->type
= PIPE_DRIVER_QUERY_TYPE_VOLTS
;
1324 else if (sscanf(name
, "sensors_curr_cu-%s", arg_name
) == 1) {
1325 hud_sensors_temp_graph_install(pane
, arg_name
,
1326 SENSORS_CURRENT_CURRENT
);
1327 pane
->type
= PIPE_DRIVER_QUERY_TYPE_AMPS
;
1329 else if (sscanf(name
, "sensors_pow_cu-%s", arg_name
) == 1) {
1330 hud_sensors_temp_graph_install(pane
, arg_name
,
1331 SENSORS_POWER_CURRENT
);
1332 pane
->type
= PIPE_DRIVER_QUERY_TYPE_WATTS
;
1335 else if (strcmp(name
, "samples-passed") == 0 &&
1336 has_occlusion_query(screen
)) {
1337 hud_pipe_query_install(&hud
->batch_query
, pane
,
1339 PIPE_QUERY_OCCLUSION_COUNTER
, 0, 0,
1340 PIPE_DRIVER_QUERY_TYPE_UINT64
,
1341 PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE
,
1344 else if (strcmp(name
, "primitives-generated") == 0 &&
1345 has_streamout(screen
)) {
1346 hud_pipe_query_install(&hud
->batch_query
, pane
,
1347 "primitives-generated",
1348 PIPE_QUERY_PRIMITIVES_GENERATED
, 0, 0,
1349 PIPE_DRIVER_QUERY_TYPE_UINT64
,
1350 PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE
,
1354 boolean processed
= FALSE
;
1356 /* pipeline statistics queries */
1357 if (has_pipeline_stats_query(screen
)) {
1358 static const char *pipeline_statistics_names
[] =
1365 "clipper-invocations",
1366 "clipper-primitives-generated",
1372 for (i
= 0; i
< ARRAY_SIZE(pipeline_statistics_names
); ++i
)
1373 if (strcmp(name
, pipeline_statistics_names
[i
]) == 0)
1375 if (i
< ARRAY_SIZE(pipeline_statistics_names
)) {
1376 hud_pipe_query_install(&hud
->batch_query
, pane
, name
,
1377 PIPE_QUERY_PIPELINE_STATISTICS
, i
,
1378 0, PIPE_DRIVER_QUERY_TYPE_UINT64
,
1379 PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE
,
1385 /* driver queries */
1387 if (!hud_driver_query_install(&hud
->batch_query
, pane
,
1389 fprintf(stderr
, "gallium_hud: unknown driver query '%s'\n", name
);
1400 fprintf(stderr
, "gallium_hud: syntax error: unexpected ':', "
1401 "expected a name\n");
1406 num
= parse_string(env
, s
);
1409 if (num
&& sscanf(s
, "%u", &i
) == 1) {
1410 hud_pane_set_max_value(pane
, i
);
1411 pane
->initial_max_value
= i
;
1414 fprintf(stderr
, "gallium_hud: syntax error: unexpected '%c' (%i) "
1415 "after ':'\n", *env
, *env
);
1424 fprintf(stderr
, "gallium_hud: syntax error: unexpected '=', "
1425 "expected a name\n");
1430 num
= parse_string(env
, s
);
1434 if (added
&& !LIST_IS_EMPTY(&pane
->graph_list
)) {
1435 struct hud_graph
*graph
;
1436 graph
= LIST_ENTRY(struct hud_graph
, pane
->graph_list
.prev
, head
);
1437 strncpy(graph
->name
, s
, sizeof(graph
->name
)-1);
1438 graph
->name
[sizeof(graph
->name
)-1] = 0;
1445 /* parse a separator */
1456 y
+= height
+ hud
->font
.glyph_height
* (pane
->num_graphs
+ 2);
1457 y_simple
+= hud
->font
.glyph_height
* (pane
->num_graphs
+ 1);
1460 if (pane
&& pane
->num_graphs
) {
1461 list_addtail(&pane
->head
, &hud
->pane_list
);
1470 x
+= column_width
+ hud
->font
.glyph_width
* 9;
1473 if (pane
&& pane
->num_graphs
) {
1474 list_addtail(&pane
->head
, &hud
->pane_list
);
1478 /* Starting a new column; reset column width. */
1483 fprintf(stderr
, "gallium_hud: syntax error: unexpected '%c'\n", *env
);
1487 /* Reset to defaults for the next pane in case these were modified. */
1489 ceiling
= UINT64_MAX
;
1490 dyn_ceiling
= false;
1496 if (pane
->num_graphs
) {
1497 list_addtail(&pane
->head
, &hud
->pane_list
);
1504 LIST_FOR_EACH_ENTRY(pane
, &hud
->pane_list
, head
) {
1505 struct hud_graph
*gr
;
1507 LIST_FOR_EACH_ENTRY(gr
, &pane
->graph_list
, head
) {
1508 hud_graph_set_dump_file(gr
);
1514 print_help(struct pipe_screen
*screen
)
1516 int i
, num_queries
, num_cpus
= hud_get_num_cpus();
1518 puts("Syntax: GALLIUM_HUD=name1[+name2][...][:value1][,nameI...][;nameJ...]");
1520 puts(" Names are identifiers of data sources which will be drawn as graphs");
1521 puts(" in panes. Multiple graphs can be drawn in the same pane.");
1522 puts(" There can be multiple panes placed in rows and columns.");
1524 puts(" '+' separates names which will share a pane.");
1525 puts(" ':[value]' specifies the initial maximum value of the Y axis");
1526 puts(" for the given pane.");
1527 puts(" ',' creates a new pane below the last one.");
1528 puts(" ';' creates a new pane at the top of the next column.");
1529 puts(" '=' followed by a string, changes the name of the last data source");
1530 puts(" to that string");
1532 puts(" Example: GALLIUM_HUD=\"cpu,fps;primitives-generated\"");
1534 puts(" Additionally, by prepending '.[identifier][value]' modifiers to");
1535 puts(" a name, it is possible to explicitly set the location and size");
1536 puts(" of a pane, along with limiting overall maximum value of the");
1537 puts(" Y axis and activating dynamic readjustment of the Y axis.");
1538 puts(" Several modifiers may be applied to the same pane simultaneously.");
1540 puts(" 'x[value]' sets the location of the pane on the x axis relative");
1541 puts(" to the upper-left corner of the viewport, in pixels.");
1542 puts(" 'y[value]' sets the location of the pane on the y axis relative");
1543 puts(" to the upper-left corner of the viewport, in pixels.");
1544 puts(" 'w[value]' sets width of the graph pixels.");
1545 puts(" 'h[value]' sets height of the graph in pixels.");
1546 puts(" 'c[value]' sets the ceiling of the value of the Y axis.");
1547 puts(" If the graph needs to draw values higher than");
1548 puts(" the ceiling allows, the value is clamped.");
1549 puts(" 'd' activates dynamic Y axis readjustment to set the value of");
1550 puts(" the Y axis to match the highest value still visible in the graph.");
1551 puts(" 'r' resets the color counter (the next color will be green)");
1552 puts(" 's' sort items below graphs in descending order");
1554 puts(" If 'c' and 'd' modifiers are used simultaneously, both are in effect:");
1555 puts(" the Y axis does not go above the restriction imposed by 'c' while");
1556 puts(" still adjusting the value of the Y axis down when appropriate.");
1558 puts(" You can change behavior of the whole HUD by adding these options at");
1559 puts(" the beginning of the environment variable:");
1560 puts(" 'simple,' disables all the fancy stuff and only draws text.");
1562 puts(" Example: GALLIUM_HUD=\".w256.h64.x1600.y520.d.c1000fps+cpu,.datom-count\"");
1564 puts(" Available names:");
1569 for (i
= 0; i
< num_cpus
; i
++)
1570 printf(" cpu%i\n", i
);
1572 if (has_occlusion_query(screen
))
1573 puts(" samples-passed");
1574 if (has_streamout(screen
))
1575 puts(" primitives-generated");
1577 if (has_pipeline_stats_query(screen
)) {
1578 puts(" ia-vertices");
1579 puts(" ia-primitives");
1580 puts(" vs-invocations");
1581 puts(" gs-invocations");
1582 puts(" gs-primitives");
1583 puts(" clipper-invocations");
1584 puts(" clipper-primitives-generated");
1585 puts(" ps-invocations");
1586 puts(" hs-invocations");
1587 puts(" ds-invocations");
1588 puts(" cs-invocations");
1591 #ifdef HAVE_GALLIUM_EXTRA_HUD
1592 hud_get_num_disks(1);
1593 hud_get_num_nics(1);
1594 hud_get_num_cpufreq(1);
1596 #ifdef HAVE_LIBSENSORS
1597 hud_get_num_sensors(1);
1600 if (screen
->get_driver_query_info
){
1601 boolean skipping
= false;
1602 struct pipe_driver_query_info info
;
1603 num_queries
= screen
->get_driver_query_info(screen
, 0, NULL
);
1605 for (i
= 0; i
< num_queries
; i
++){
1606 screen
->get_driver_query_info(screen
, i
, &info
);
1607 if (info
.flags
& PIPE_DRIVER_QUERY_FLAG_DONT_LIST
) {
1612 printf(" %s\n", info
.name
);
1623 hud_unset_draw_context(struct hud_context
*hud
)
1625 struct pipe_context
*pipe
= hud
->pipe
;
1630 pipe_sampler_view_reference(&hud
->font_sampler_view
, NULL
);
1632 if (hud
->fs_color
) {
1633 pipe
->delete_fs_state(pipe
, hud
->fs_color
);
1634 hud
->fs_color
= NULL
;
1637 pipe
->delete_fs_state(pipe
, hud
->fs_text
);
1638 hud
->fs_text
= NULL
;
1641 pipe
->delete_vs_state(pipe
, hud
->vs
);
1650 hud_set_draw_context(struct hud_context
*hud
, struct cso_context
*cso
)
1652 struct pipe_context
*pipe
= cso_get_pipe_context(cso
);
1658 struct pipe_sampler_view view_templ
;
1659 u_sampler_view_default_template(
1660 &view_templ
, hud
->font
.texture
, hud
->font
.texture
->format
);
1661 hud
->font_sampler_view
= pipe
->create_sampler_view(pipe
, hud
->font
.texture
,
1663 if (!hud
->font_sampler_view
)
1666 /* fragment shader */
1668 util_make_fragment_passthrough_shader(pipe
,
1669 TGSI_SEMANTIC_COLOR
,
1670 TGSI_INTERPOLATE_CONSTANT
,
1674 /* Read a texture and do .xxxx swizzling. */
1675 static const char *fragment_shader_text
= {
1677 "DCL IN[0], GENERIC[0], LINEAR\n"
1679 "DCL SVIEW[0], RECT, FLOAT\n"
1680 "DCL OUT[0], COLOR[0]\n"
1683 "TEX TEMP[0], IN[0], SAMP[0], RECT\n"
1684 "MOV OUT[0], TEMP[0].xxxx\n"
1688 struct tgsi_token tokens
[1000];
1689 struct pipe_shader_state state
;
1691 if (!tgsi_text_translate(fragment_shader_text
, tokens
, ARRAY_SIZE(tokens
))) {
1695 pipe_shader_state_from_tgsi(&state
, tokens
);
1696 hud
->fs_text
= pipe
->create_fs_state(pipe
, &state
);
1701 static const char *vertex_shader_text
= {
1704 "DCL OUT[0], POSITION\n"
1705 "DCL OUT[1], COLOR[0]\n" /* color */
1706 "DCL OUT[2], GENERIC[0]\n" /* texcoord */
1708 * [1] = (2/fb_width, 2/fb_height, xoffset, yoffset)
1709 * [2] = (xscale, yscale, 0, 0) */
1710 "DCL CONST[0][0..2]\n"
1712 "IMM[0] FLT32 { -1, 0, 0, 1 }\n"
1714 /* v = in * (xscale, yscale) + (xoffset, yoffset) */
1715 "MAD TEMP[0].xy, IN[0], CONST[0][2].xyyy, CONST[0][1].zwww\n"
1716 /* pos = v * (2 / fb_width, 2 / fb_height) - (1, 1) */
1717 "MAD OUT[0].xy, TEMP[0], CONST[0][1].xyyy, IMM[0].xxxx\n"
1718 "MOV OUT[0].zw, IMM[0]\n"
1720 "MOV OUT[1], CONST[0][0]\n"
1721 "MOV OUT[2], IN[1]\n"
1725 struct tgsi_token tokens
[1000];
1726 struct pipe_shader_state state
;
1727 if (!tgsi_text_translate(vertex_shader_text
, tokens
, ARRAY_SIZE(tokens
))) {
1731 pipe_shader_state_from_tgsi(&state
, tokens
);
1732 hud
->vs
= pipe
->create_vs_state(pipe
, &state
);
1738 hud_unset_draw_context(hud
);
1739 fprintf(stderr
, "hud: failed to set a draw context");
1744 hud_unset_record_context(struct hud_context
*hud
)
1746 struct pipe_context
*pipe
= hud
->record_pipe
;
1747 struct hud_pane
*pane
, *pane_tmp
;
1748 struct hud_graph
*graph
, *graph_tmp
;
1753 LIST_FOR_EACH_ENTRY_SAFE(pane
, pane_tmp
, &hud
->pane_list
, head
) {
1754 LIST_FOR_EACH_ENTRY_SAFE(graph
, graph_tmp
, &pane
->graph_list
, head
) {
1755 LIST_DEL(&graph
->head
);
1756 hud_graph_destroy(graph
, pipe
);
1758 LIST_DEL(&pane
->head
);
1762 hud_batch_query_cleanup(&hud
->batch_query
, pipe
);
1763 hud
->record_pipe
= NULL
;
1767 hud_set_record_context(struct hud_context
*hud
, struct pipe_context
*pipe
)
1769 hud
->record_pipe
= pipe
;
1775 * If "share" is non-NULL and GALLIUM_HUD_SHARE=x,y is set, increment the
1776 * reference counter of "share", set "cso" as the recording or drawing context
1777 * according to the environment variable, and return "share".
1778 * This allows sharing the HUD instance within a multi-context share group,
1779 * record queries in one context and draw them in another.
1781 struct hud_context
*
1782 hud_create(struct cso_context
*cso
, struct hud_context
*share
)
1784 const char *share_env
= debug_get_option("GALLIUM_HUD_SHARE", NULL
);
1785 unsigned record_ctx
= 0, draw_ctx
= 0;
1787 if (share_env
&& sscanf(share_env
, "%u,%u", &record_ctx
, &draw_ctx
) != 2)
1790 if (share
&& share_env
) {
1791 /* All contexts in a share group share the HUD instance.
1792 * Only one context can record queries and only one context
1795 * GALLIUM_HUD_SHARE=x,y determines the context indices.
1797 int context_id
= p_atomic_inc_return(&share
->refcount
) - 1;
1799 if (context_id
== record_ctx
) {
1800 assert(!share
->record_pipe
);
1801 hud_set_record_context(share
, cso_get_pipe_context(cso
));
1804 if (context_id
== draw_ctx
) {
1805 assert(!share
->pipe
);
1806 hud_set_draw_context(share
, cso
);
1812 struct pipe_screen
*screen
= cso_get_pipe_context(cso
)->screen
;
1813 struct hud_context
*hud
;
1815 const char *env
= debug_get_option("GALLIUM_HUD", NULL
);
1817 unsigned signo
= debug_get_num_option("GALLIUM_HUD_TOGGLE_SIGNAL", 0);
1818 static boolean sig_handled
= FALSE
;
1819 struct sigaction action
= {};
1821 huds_visible
= debug_get_bool_option("GALLIUM_HUD_VISIBLE", TRUE
);
1826 if (strcmp(env
, "help") == 0) {
1831 hud
= CALLOC_STRUCT(hud_context
);
1835 /* font (the context is only used for the texture upload) */
1836 if (!util_font_create(cso_get_pipe_context(cso
),
1837 UTIL_FONT_FIXED_8X13
, &hud
->font
)) {
1843 hud
->has_srgb
= screen
->is_format_supported(screen
,
1844 PIPE_FORMAT_B8G8R8A8_SRGB
,
1845 PIPE_TEXTURE_2D
, 0, 0,
1846 PIPE_BIND_RENDER_TARGET
) != 0;
1849 hud
->no_blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
1851 hud
->alpha_blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
1852 hud
->alpha_blend
.rt
[0].blend_enable
= 1;
1853 hud
->alpha_blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
1854 hud
->alpha_blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
1855 hud
->alpha_blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
1856 hud
->alpha_blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
1857 hud
->alpha_blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ZERO
;
1858 hud
->alpha_blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ONE
;
1861 hud
->rasterizer
.half_pixel_center
= 1;
1862 hud
->rasterizer
.bottom_edge_rule
= 1;
1863 hud
->rasterizer
.depth_clip_near
= 1;
1864 hud
->rasterizer
.depth_clip_far
= 1;
1865 hud
->rasterizer
.line_width
= 1;
1866 hud
->rasterizer
.line_last_pixel
= 1;
1868 hud
->rasterizer_aa_lines
= hud
->rasterizer
;
1869 hud
->rasterizer_aa_lines
.line_smooth
= 1;
1871 /* vertex elements */
1872 for (i
= 0; i
< 2; i
++) {
1873 hud
->velems
[i
].src_offset
= i
* 2 * sizeof(float);
1874 hud
->velems
[i
].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
1875 hud
->velems
[i
].vertex_buffer_index
= 0;
1878 /* sampler state (for font drawing) */
1879 hud
->font_sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
1880 hud
->font_sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
1881 hud
->font_sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
1882 hud
->font_sampler_state
.normalized_coords
= 0;
1885 hud
->constbuf
.buffer_size
= sizeof(hud
->constants
);
1886 hud
->constbuf
.user_buffer
= &hud
->constants
;
1888 list_inithead(&hud
->pane_list
);
1890 /* setup sig handler once for all hud contexts */
1892 if (!sig_handled
&& signo
!= 0) {
1893 action
.sa_sigaction
= &signal_visible_handler
;
1894 action
.sa_flags
= SA_SIGINFO
;
1897 fprintf(stderr
, "gallium_hud: invalid signal %u\n", signo
);
1898 else if (sigaction(signo
, &action
, NULL
) < 0)
1899 fprintf(stderr
, "gallium_hud: unable to set handler for signal %u\n", signo
);
1906 if (record_ctx
== 0)
1907 hud_set_record_context(hud
, cso_get_pipe_context(cso
));
1909 hud_set_draw_context(hud
, cso
);
1911 hud_parse_env_var(hud
, screen
, env
);
1916 * Destroy a HUD. If the HUD has several users, decrease the reference counter
1917 * and detach the context from the HUD.
1920 hud_destroy(struct hud_context
*hud
, struct cso_context
*cso
)
1922 if (!cso
|| hud
->record_pipe
== cso_get_pipe_context(cso
))
1923 hud_unset_record_context(hud
);
1925 if (!cso
|| hud
->cso
== cso
)
1926 hud_unset_draw_context(hud
);
1928 if (p_atomic_dec_zero(&hud
->refcount
)) {
1929 pipe_resource_reference(&hud
->font
.texture
, NULL
);
1935 hud_add_queue_for_monitoring(struct hud_context
*hud
,
1936 struct util_queue_monitoring
*queue_info
)
1938 assert(!hud
->monitored_queue
);
1939 hud
->monitored_queue
= queue_info
;