1 /**************************************************************************
2 *
3 * Copyright 2013 Marek Olšák <[email protected]>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL 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.
25 *
26 **************************************************************************/
27
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 gallium frontend.
31 *
32 * The HUD is controlled with the GALLIUM_HUD environment variable.
33 * Set GALLIUM_HUD=help for more info.
34 */
35
36 #include <inttypes.h>
37 #include <signal.h>
38 #include <stdio.h>
39
40 #include "util/detect_os.h"
41
42 #if DETECT_OS_WINDOWS
43 #include <io.h>
44
45 /**
46 * Access flags W_OK are defined by mingw, but not defined by MSVC, we defined it according to
47 * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/access-waccess
48 */
49 #ifndef W_OK
50 #define W_OK 02
51 #endif
52 #endif /* DETECT_OS_WINDOWS */
53
54 #include "hud/hud_context.h"
55 #include "hud/hud_private.h"
56
57 #include "frontend/api.h"
58 #include "cso_cache/cso_context.h"
59 #include "util/u_draw_quad.h"
60 #include "util/format/u_format.h"
61 #include "util/u_inlines.h"
62 #include "util/u_memory.h"
63 #include "util/u_math.h"
64 #include "util/u_sampler.h"
65 #include "util/u_simple_shaders.h"
66 #include "util/u_string.h"
67 #include "util/u_upload_mgr.h"
68 #include "tgsi/tgsi_text.h"
69 #include "tgsi/tgsi_dump.h"
70
71 #define HUD_DEFAULT_VISIBILITY true
72 #define HUD_DEFAULT_SCALE 1
73 #define HUD_DEFAULT_ROTATION 0
74 #define HUD_DEFAULT_OPACITY 66
75
76 /* Control the visibility of all HUD contexts */
77 static bool huds_visible = HUD_DEFAULT_VISIBILITY;
78 static int hud_scale = HUD_DEFAULT_SCALE;
79 static int hud_rotate = HUD_DEFAULT_ROTATION;
80 static float hud_opacity = HUD_DEFAULT_OPACITY / 100.0f;
81
82 #if DETECT_OS_POSIX
83 static void
signal_visible_handler(int sig,siginfo_t * siginfo,void * context)84 signal_visible_handler(int sig, siginfo_t *siginfo, void *context)
85 {
86 huds_visible = !huds_visible;
87 }
88 #endif
89
90 static void
hud_draw_colored_prims(struct hud_context * hud,unsigned prim,float * buffer,unsigned num_vertices,float r,float g,float b,float a,int xoffset,int yoffset,float yscale)91 hud_draw_colored_prims(struct hud_context *hud, unsigned prim,
92 float *buffer, unsigned num_vertices,
93 float r, float g, float b, float a,
94 int xoffset, int yoffset, float yscale)
95 {
96 struct cso_context *cso = hud->cso;
97 struct pipe_context *pipe = hud->pipe;
98 struct pipe_vertex_buffer vbuffer = {0};
99
100 hud->constants.color[0] = r;
101 hud->constants.color[1] = g;
102 hud->constants.color[2] = b;
103 hud->constants.color[3] = a;
104 hud->constants.translate[0] = (float) (xoffset * hud_scale);
105 hud->constants.translate[1] = (float) (yoffset * hud_scale);
106 hud->constants.scale[0] = hud_scale;
107 hud->constants.scale[1] = yscale * hud_scale;
108 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, &hud->constbuf);
109
110 u_upload_data(hud->pipe->stream_uploader, 0,
111 num_vertices * 2 * sizeof(float), 16, buffer,
112 &vbuffer.buffer_offset, &vbuffer.buffer.resource);
113 u_upload_unmap(hud->pipe->stream_uploader);
114
115 cso_set_vertex_buffers(cso, 1, true, &vbuffer);
116 cso_set_fragment_shader_handle(hud->cso, hud->fs_color);
117 cso_draw_arrays(cso, prim, 0, num_vertices);
118 }
119
120 static void
hud_draw_colored_quad(struct hud_context * hud,unsigned prim,unsigned x1,unsigned y1,unsigned x2,unsigned y2,float r,float g,float b,float a)121 hud_draw_colored_quad(struct hud_context *hud, unsigned prim,
122 unsigned x1, unsigned y1, unsigned x2, unsigned y2,
123 float r, float g, float b, float a)
124 {
125 float buffer[] = {
126 (float) x1, (float) y1,
127 (float) x1, (float) y2,
128 (float) x2, (float) y2,
129 (float) x2, (float) y1,
130 };
131
132 hud_draw_colored_prims(hud, prim, buffer, 4, r, g, b, a, 0, 0, 1);
133 }
134
135 static void
hud_draw_background_quad(struct hud_context * hud,unsigned x1,unsigned y1,unsigned x2,unsigned y2)136 hud_draw_background_quad(struct hud_context *hud,
137 unsigned x1, unsigned y1, unsigned x2, unsigned y2)
138 {
139 float *vertices = hud->bg.vertices + hud->bg.num_vertices*2;
140 unsigned num = 0;
141
142 assert(hud->bg.num_vertices + 4 <= hud->bg.max_num_vertices);
143
144 vertices[num++] = (float) x1;
145 vertices[num++] = (float) y1;
146
147 vertices[num++] = (float) x1;
148 vertices[num++] = (float) y2;
149
150 vertices[num++] = (float) x2;
151 vertices[num++] = (float) y2;
152
153 vertices[num++] = (float) x2;
154 vertices[num++] = (float) y1;
155
156 hud->bg.num_vertices += num/2;
157 }
158
159 static void
hud_draw_string(struct hud_context * hud,unsigned x,unsigned y,const char * str,...)160 hud_draw_string(struct hud_context *hud, unsigned x, unsigned y,
161 const char *str, ...)
162 {
163 char buf[256];
164 char *s = buf;
165 float *vertices = hud->text.vertices + hud->text.num_vertices*4;
166 unsigned num = 0;
167
168 va_list ap;
169 va_start(ap, str);
170 vsnprintf(buf, sizeof(buf), str, ap);
171 va_end(ap);
172
173 if (!*s)
174 return;
175
176 hud_draw_background_quad(hud,
177 x, y,
178 x + strlen(buf)*hud->font.glyph_width,
179 y + hud->font.glyph_height);
180
181 while (*s) {
182 unsigned x1 = x;
183 unsigned y1 = y;
184 unsigned x2 = x + hud->font.glyph_width;
185 unsigned y2 = y + hud->font.glyph_height;
186 unsigned tx1 = (*s % 16) * hud->font.glyph_width;
187 unsigned ty1 = (*s / 16) * hud->font.glyph_height;
188 unsigned tx2 = tx1 + hud->font.glyph_width;
189 unsigned ty2 = ty1 + hud->font.glyph_height;
190
191 if (*s == ' ') {
192 x += hud->font.glyph_width;
193 s++;
194 continue;
195 }
196
197 assert(hud->text.num_vertices + num/4 + 4 <= hud->text.max_num_vertices);
198
199 vertices[num++] = (float) x1;
200 vertices[num++] = (float) y1;
201 vertices[num++] = (float) tx1;
202 vertices[num++] = (float) ty1;
203
204 vertices[num++] = (float) x1;
205 vertices[num++] = (float) y2;
206 vertices[num++] = (float) tx1;
207 vertices[num++] = (float) ty2;
208
209 vertices[num++] = (float) x2;
210 vertices[num++] = (float) y2;
211 vertices[num++] = (float) tx2;
212 vertices[num++] = (float) ty2;
213
214 vertices[num++] = (float) x2;
215 vertices[num++] = (float) y1;
216 vertices[num++] = (float) tx2;
217 vertices[num++] = (float) ty1;
218
219 x += hud->font.glyph_width;
220 s++;
221 }
222
223 hud->text.num_vertices += num/4;
224 }
225
226 static const char *
get_float_modifier(double d)227 get_float_modifier(double d)
228 {
229 /* Round to 3 decimal places so as not to print trailing zeros. */
230 if (d*1000 != (int)(d*1000))
231 d = round(d * 1000) / 1000;
232
233 /* Show at least 4 digits with at most 3 decimal places, but not zeros. */
234 if (d >= 1000 || d == (int)d)
235 return "%.0f";
236 else if (d >= 100 || d*10 == (int)(d*10))
237 return "%.1f";
238 else if (d >= 10 || d*100 == (int)(d*100))
239 return "%.2f";
240 else
241 return "%.3f";
242 }
243
244 static void
number_to_human_readable(double num,enum pipe_driver_query_type type,char * out)245 number_to_human_readable(double num, enum pipe_driver_query_type type,
246 char *out)
247 {
248 static const char *byte_units[] =
249 {" B", " KB", " MB", " GB", " TB", " PB", " EB"};
250 static const char *metric_units[] =
251 {"", " k", " M", " G", " T", " P", " E"};
252 static const char *time_units[] =
253 {" us", " ms", " s"}; /* based on microseconds */
254 static const char *hz_units[] =
255 {" Hz", " KHz", " MHz", " GHz"};
256 static const char *percent_units[] = {"%"};
257 static const char *dbm_units[] = {" (-dBm)"};
258 static const char *temperature_units[] = {" C"};
259 static const char *volt_units[] = {" mV", " V"};
260 static const char *amp_units[] = {" mA", " A"};
261 static const char *watt_units[] = {" mW", " W"};
262 static const char *float_units[] = {""};
263
264 const char **units;
265 unsigned max_unit;
266 double divisor = (type == PIPE_DRIVER_QUERY_TYPE_BYTES) ? 1024 : 1000;
267 unsigned unit = 0;
268 double d = num;
269
270 switch (type) {
271 case PIPE_DRIVER_QUERY_TYPE_MICROSECONDS:
272 max_unit = ARRAY_SIZE(time_units)-1;
273 units = time_units;
274 break;
275 case PIPE_DRIVER_QUERY_TYPE_VOLTS:
276 max_unit = ARRAY_SIZE(volt_units)-1;
277 units = volt_units;
278 break;
279 case PIPE_DRIVER_QUERY_TYPE_AMPS:
280 max_unit = ARRAY_SIZE(amp_units)-1;
281 units = amp_units;
282 break;
283 case PIPE_DRIVER_QUERY_TYPE_DBM:
284 max_unit = ARRAY_SIZE(dbm_units)-1;
285 units = dbm_units;
286 break;
287 case PIPE_DRIVER_QUERY_TYPE_TEMPERATURE:
288 max_unit = ARRAY_SIZE(temperature_units)-1;
289 units = temperature_units;
290 break;
291 case PIPE_DRIVER_QUERY_TYPE_FLOAT:
292 max_unit = ARRAY_SIZE(float_units)-1;
293 units = float_units;
294 break;
295 case PIPE_DRIVER_QUERY_TYPE_PERCENTAGE:
296 max_unit = ARRAY_SIZE(percent_units)-1;
297 units = percent_units;
298 break;
299 case PIPE_DRIVER_QUERY_TYPE_BYTES:
300 max_unit = ARRAY_SIZE(byte_units)-1;
301 units = byte_units;
302 break;
303 case PIPE_DRIVER_QUERY_TYPE_HZ:
304 max_unit = ARRAY_SIZE(hz_units)-1;
305 units = hz_units;
306 break;
307 case PIPE_DRIVER_QUERY_TYPE_WATTS:
308 max_unit = ARRAY_SIZE(watt_units)-1;
309 units = watt_units;
310 break;
311 default:
312 max_unit = ARRAY_SIZE(metric_units)-1;
313 units = metric_units;
314 }
315
316 while (d > divisor && unit < max_unit) {
317 d /= divisor;
318 unit++;
319 }
320 int n = sprintf(out, get_float_modifier(d), d);
321 if (n > 0)
322 sprintf(&out[n], "%s", units[unit]);
323 }
324
325 static void
hud_draw_graph_line_strip(struct hud_context * hud,const struct hud_graph * gr,unsigned xoffset,unsigned yoffset,float yscale)326 hud_draw_graph_line_strip(struct hud_context *hud, const struct hud_graph *gr,
327 unsigned xoffset, unsigned yoffset, float yscale)
328 {
329 if (gr->num_vertices <= 1)
330 return;
331
332 assert(gr->index <= gr->num_vertices);
333
334 hud_draw_colored_prims(hud, MESA_PRIM_LINE_STRIP,
335 gr->vertices, gr->index,
336 gr->color[0], gr->color[1], gr->color[2], 1,
337 xoffset + (gr->pane->max_num_vertices - gr->index - 1) * 2 - 1,
338 yoffset, yscale);
339
340 if (gr->num_vertices <= gr->index)
341 return;
342
343 hud_draw_colored_prims(hud, MESA_PRIM_LINE_STRIP,
344 gr->vertices + gr->index*2,
345 gr->num_vertices - gr->index,
346 gr->color[0], gr->color[1], gr->color[2], 1,
347 xoffset - gr->index*2 - 1, yoffset, yscale);
348 }
349
350 static void
hud_pane_accumulate_vertices(struct hud_context * hud,const struct hud_pane * pane)351 hud_pane_accumulate_vertices(struct hud_context *hud,
352 const struct hud_pane *pane)
353 {
354 struct hud_graph *gr;
355 float *line_verts = hud->whitelines.vertices + hud->whitelines.num_vertices*2;
356 unsigned i, num = 0;
357 char str[32];
358 const unsigned last_line = pane->last_line;
359
360 /* draw background */
361 hud_draw_background_quad(hud,
362 pane->x1, pane->y1,
363 pane->x2, pane->y2);
364
365 /* draw numbers on the right-hand side */
366 for (i = 0; i <= last_line; i++) {
367 unsigned x = pane->x2 + 2;
368 unsigned y = pane->inner_y1 +
369 pane->inner_height * (last_line - i) / last_line -
370 hud->font.glyph_height / 2;
371
372 number_to_human_readable(pane->max_value * i / last_line,
373 pane->type, str);
374 hud_draw_string(hud, x, y, "%s", str);
375 }
376
377 /* draw info below the pane */
378 i = 0;
379 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
380 unsigned x = pane->x1 + 2;
381 unsigned y = pane->y2 + 2 + i*hud->font.glyph_height;
382
383 number_to_human_readable(gr->current_value, pane->type, str);
384 hud_draw_string(hud, x, y, " %s: %s", gr->name, str);
385 i++;
386 }
387
388 /* draw border */
389 assert(hud->whitelines.num_vertices + num/2 + 8 <= hud->whitelines.max_num_vertices);
390 line_verts[num++] = (float) pane->x1;
391 line_verts[num++] = (float) pane->y1;
392 line_verts[num++] = (float) pane->x2;
393 line_verts[num++] = (float) pane->y1;
394
395 line_verts[num++] = (float) pane->x2;
396 line_verts[num++] = (float) pane->y1;
397 line_verts[num++] = (float) pane->x2;
398 line_verts[num++] = (float) pane->y2;
399
400 line_verts[num++] = (float) pane->x1;
401 line_verts[num++] = (float) pane->y2;
402 line_verts[num++] = (float) pane->x2;
403 line_verts[num++] = (float) pane->y2;
404
405 line_verts[num++] = (float) pane->x1;
406 line_verts[num++] = (float) pane->y1;
407 line_verts[num++] = (float) pane->x1;
408 line_verts[num++] = (float) pane->y2;
409
410 /* draw horizontal lines inside the graph */
411 for (i = 0; i <= last_line; i++) {
412 float y = round((pane->max_value * i / (double)last_line) *
413 pane->yscale + pane->inner_y2);
414
415 assert(hud->whitelines.num_vertices + num/2 + 2 <= hud->whitelines.max_num_vertices);
416 line_verts[num++] = pane->x1;
417 line_verts[num++] = y;
418 line_verts[num++] = pane->x2;
419 line_verts[num++] = y;
420 }
421
422 hud->whitelines.num_vertices += num/2;
423 }
424
425 static void
hud_pane_accumulate_vertices_simple(struct hud_context * hud,const struct hud_pane * pane)426 hud_pane_accumulate_vertices_simple(struct hud_context *hud,
427 const struct hud_pane *pane)
428 {
429 struct hud_graph *gr;
430 unsigned i;
431 char str[32];
432
433 /* draw info below the pane */
434 i = 0;
435 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
436 unsigned x = pane->x1;
437 unsigned y = pane->y_simple + i*hud->font.glyph_height;
438
439 number_to_human_readable(gr->current_value, pane->type, str);
440 hud_draw_string(hud, x, y, "%s: %s", gr->name, str);
441 i++;
442 }
443 }
444
445 static void
hud_pane_draw_colored_objects(struct hud_context * hud,const struct hud_pane * pane)446 hud_pane_draw_colored_objects(struct hud_context *hud,
447 const struct hud_pane *pane)
448 {
449 struct hud_graph *gr;
450 unsigned i;
451
452 /* draw colored quads below the pane */
453 i = 0;
454 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
455 unsigned x = pane->x1 + 2;
456 unsigned y = pane->y2 + 2 + i*hud->font.glyph_height;
457
458 hud_draw_colored_quad(hud, MESA_PRIM_QUADS, x + 1, y + 1, x + 12, y + 13,
459 gr->color[0], gr->color[1], gr->color[2], 1);
460 i++;
461 }
462
463 /* draw the line strips */
464 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
465 hud_draw_graph_line_strip(hud, gr, pane->inner_x1, pane->inner_y2, pane->yscale);
466 }
467 }
468
469 static void
hud_prepare_vertices(struct hud_context * hud,struct vertex_queue * v,unsigned num_vertices,unsigned stride)470 hud_prepare_vertices(struct hud_context *hud, struct vertex_queue *v,
471 unsigned num_vertices, unsigned stride)
472 {
473 v->num_vertices = 0;
474 v->max_num_vertices = num_vertices;
475 v->buffer_size = stride * num_vertices;
476 }
477
478 /**
479 * Draw the HUD to the texture \p tex.
480 * The texture is usually the back buffer being displayed.
481 */
482 static void
hud_draw_results(struct hud_context * hud,struct pipe_resource * tex)483 hud_draw_results(struct hud_context *hud, struct pipe_resource *tex)
484 {
485 struct cso_context *cso = hud->cso;
486 struct pipe_context *pipe = hud->pipe;
487 struct pipe_framebuffer_state fb;
488 struct pipe_surface surf_templ, *surf;
489 struct pipe_viewport_state viewport;
490 const struct pipe_sampler_state *sampler_states[] =
491 { &hud->font_sampler_state };
492 struct hud_pane *pane;
493
494 if (!huds_visible)
495 return;
496
497 hud->fb_width = tex->width0;
498 hud->fb_height = tex->height0;
499 float th = hud_rotate * (M_PI / 180.0f);
500 hud->constants.rotate[0] = cos(th);
501 hud->constants.rotate[1] = -sin(th);
502 hud->constants.rotate[2] = sin(th);
503 hud->constants.rotate[3] = cos(th);
504
505 /* invert the aspect ratio when we rotate the hud */
506 if (hud_rotate % 180 == 90) {
507 hud->constants.two_div_fb_height = 2.0f / hud->fb_width;
508 hud->constants.two_div_fb_width = 2.0f / hud->fb_height;
509 } else {
510 assert(hud_rotate % 180 == 0);
511 hud->constants.two_div_fb_width = 2.0f / hud->fb_width;
512 hud->constants.two_div_fb_height = 2.0f / hud->fb_height;
513 }
514
515 cso_save_state(cso, (CSO_BIT_FRAMEBUFFER |
516 CSO_BIT_SAMPLE_MASK |
517 CSO_BIT_MIN_SAMPLES |
518 CSO_BIT_BLEND |
519 CSO_BIT_DEPTH_STENCIL_ALPHA |
520 CSO_BIT_FRAGMENT_SHADER |
521 CSO_BIT_FRAGMENT_SAMPLERS |
522 CSO_BIT_RASTERIZER |
523 CSO_BIT_VIEWPORT |
524 CSO_BIT_STREAM_OUTPUTS |
525 CSO_BIT_GEOMETRY_SHADER |
526 CSO_BIT_TESSCTRL_SHADER |
527 CSO_BIT_TESSEVAL_SHADER |
528 CSO_BIT_VERTEX_SHADER |
529 CSO_BIT_VERTEX_ELEMENTS |
530 CSO_BIT_PAUSE_QUERIES |
531 CSO_BIT_RENDER_CONDITION));
532
533 /* set states */
534 memset(&surf_templ, 0, sizeof(surf_templ));
535 surf_templ.format = tex->format;
536
537 /* Without this, AA lines look thinner if they are between 2 pixels
538 * because the alpha is 0.5 on both pixels. (it's ugly)
539 *
540 * sRGB makes the width of all AA lines look the same.
541 */
542 if (hud->has_srgb) {
543 enum pipe_format srgb_format = util_format_srgb(tex->format);
544
545 if (srgb_format != PIPE_FORMAT_NONE)
546 surf_templ.format = srgb_format;
547 }
548 surf = pipe->create_surface(pipe, tex, &surf_templ);
549
550 memset(&fb, 0, sizeof(fb));
551 fb.nr_cbufs = 1;
552 fb.cbufs[0] = surf;
553 fb.zsbuf = NULL;
554 fb.width = hud->fb_width;
555 fb.height = hud->fb_height;
556 fb.resolve = NULL;
557
558 viewport.scale[0] = 0.5f * hud->fb_width;
559 viewport.scale[1] = 0.5f * hud->fb_height;
560 viewport.scale[2] = 0.0f;
561 viewport.translate[0] = 0.5f * hud->fb_width;
562 viewport.translate[1] = 0.5f * hud->fb_height;
563 viewport.translate[2] = 0.0f;
564 viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
565 viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
566 viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
567 viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
568
569 cso_set_framebuffer(cso, &fb);
570 cso_set_sample_mask(cso, ~0);
571 cso_set_min_samples(cso, 1);
572 cso_set_depth_stencil_alpha(cso, &hud->dsa);
573 cso_set_rasterizer(cso, &hud->rasterizer);
574 cso_set_viewport(cso, &viewport);
575 cso_set_stream_outputs(cso, 0, NULL, NULL);
576 cso_set_tessctrl_shader_handle(cso, NULL);
577 cso_set_tesseval_shader_handle(cso, NULL);
578 cso_set_geometry_shader_handle(cso, NULL);
579 cso_set_vertex_shader_handle(cso, hud->vs_color);
580 cso_set_vertex_elements(cso, &hud->velems);
581 cso_set_render_condition(cso, NULL, false, 0);
582 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, false,
583 &hud->font_sampler_view);
584 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, sampler_states);
585 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, &hud->constbuf);
586
587 /* draw accumulated vertices for background quads */
588 cso_set_blend(cso, &hud->alpha_blend);
589 cso_set_fragment_shader_handle(hud->cso, hud->fs_color);
590
591 if (hud->bg.num_vertices) {
592 hud->constants.color[0] = 0;
593 hud->constants.color[1] = 0;
594 hud->constants.color[2] = 0;
595 hud->constants.color[3] = hud_opacity;
596 hud->constants.translate[0] = 0;
597 hud->constants.translate[1] = 0;
598 hud->constants.scale[0] = hud_scale;
599 hud->constants.scale[1] = hud_scale;
600
601 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, &hud->constbuf);
602
603 cso_set_vertex_buffers(cso, 1, true, &hud->bg.vbuf);
604 cso_draw_arrays(cso, MESA_PRIM_QUADS, 0, hud->bg.num_vertices);
605 hud->bg.vbuf.buffer.resource = NULL;
606 } else {
607 pipe_resource_reference(&hud->bg.vbuf.buffer.resource, NULL);
608 }
609
610 /* draw accumulated vertices for text */
611 if (hud->text.num_vertices) {
612 cso_set_vertex_shader_handle(cso, hud->vs_text);
613 cso_set_vertex_elements(cso, &hud->text_velems);
614 cso_set_vertex_buffers(cso, 1, true, &hud->text.vbuf);
615 cso_set_fragment_shader_handle(hud->cso, hud->fs_text);
616 cso_draw_arrays(cso, MESA_PRIM_QUADS, 0, hud->text.num_vertices);
617 cso_set_vertex_elements(cso, &hud->velems);
618 hud->text.vbuf.buffer.resource = NULL;
619 } else {
620 pipe_resource_reference(&hud->text.vbuf.buffer.resource, NULL);
621 }
622
623 if (hud->simple)
624 goto done;
625
626 /* draw accumulated vertices for white lines */
627 cso_set_blend(cso, &hud->no_blend);
628
629 hud->constants.color[0] = 1;
630 hud->constants.color[1] = 1;
631 hud->constants.color[2] = 1;
632 hud->constants.color[3] = 1;
633 hud->constants.translate[0] = 0;
634 hud->constants.translate[1] = 0;
635 hud->constants.scale[0] = hud_scale;
636 hud->constants.scale[1] = hud_scale;
637 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, &hud->constbuf);
638
639 if (hud->whitelines.num_vertices) {
640 cso_set_vertex_shader_handle(cso, hud->vs_color);
641 cso_set_vertex_buffers(cso, 1, true, &hud->whitelines.vbuf);
642 cso_set_fragment_shader_handle(hud->cso, hud->fs_color);
643 cso_draw_arrays(cso, MESA_PRIM_LINES, 0, hud->whitelines.num_vertices);
644 hud->whitelines.vbuf.buffer.resource = NULL;
645 } else {
646 pipe_resource_reference(&hud->whitelines.vbuf.buffer.resource, NULL);
647 }
648
649 /* draw the rest */
650 cso_set_blend(cso, &hud->alpha_blend);
651 cso_set_rasterizer(cso, &hud->rasterizer_aa_lines);
652 LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
653 if (pane)
654 hud_pane_draw_colored_objects(hud, pane);
655 }
656
657 done:
658 cso_restore_state(cso, CSO_UNBIND_FS_SAMPLERVIEW0 | CSO_UNBIND_VS_CONSTANTS);
659
660 /* restore states not restored by cso */
661 if (hud->st) {
662 hud->st_invalidate_state(hud->st,
663 ST_INVALIDATE_FS_SAMPLER_VIEWS |
664 ST_INVALIDATE_VS_CONSTBUF0 |
665 ST_INVALIDATE_VERTEX_BUFFERS);
666 }
667
668 pipe_surface_reference(&surf, NULL);
669 }
670
671 static void
hud_start_queries(struct hud_context * hud,struct pipe_context * pipe)672 hud_start_queries(struct hud_context *hud, struct pipe_context *pipe)
673 {
674 struct hud_pane *pane;
675 struct hud_graph *gr;
676
677 /* Start queries. */
678 hud_batch_query_begin(hud->batch_query, pipe);
679
680 LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
681 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
682 if (gr->begin_query)
683 gr->begin_query(gr, pipe);
684 }
685 }
686 }
687
688 /* Stop queries, query results, and record vertices for charts. */
689 static void
hud_stop_queries(struct hud_context * hud,struct pipe_context * pipe)690 hud_stop_queries(struct hud_context *hud, struct pipe_context *pipe)
691 {
692 struct hud_pane *pane;
693 struct hud_graph *gr, *next;
694
695 /* prepare vertex buffers */
696 hud_prepare_vertices(hud, &hud->bg, 16 * 256, 2 * sizeof(float));
697 hud_prepare_vertices(hud, &hud->whitelines, 4 * 256, 2 * sizeof(float));
698 hud_prepare_vertices(hud, &hud->text, 16 * 1024, 4 * sizeof(float));
699
700 /* Allocate everything once and divide the storage into 3 portions
701 * manually, because u_upload_alloc can unmap memory from previous calls.
702 */
703 u_upload_alloc(pipe->stream_uploader, 0,
704 hud->bg.buffer_size +
705 hud->whitelines.buffer_size +
706 hud->text.buffer_size,
707 16, &hud->bg.vbuf.buffer_offset, &hud->bg.vbuf.buffer.resource,
708 (void**)&hud->bg.vertices);
709 if (!hud->bg.vertices)
710 return;
711
712 pipe_resource_reference(&hud->whitelines.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);
713 pipe_resource_reference(&hud->text.vbuf.buffer.resource, hud->bg.vbuf.buffer.resource);
714
715 hud->whitelines.vbuf.buffer_offset = hud->bg.vbuf.buffer_offset +
716 hud->bg.buffer_size;
717 hud->whitelines.vertices = hud->bg.vertices +
718 hud->bg.buffer_size / sizeof(float);
719
720 hud->text.vbuf.buffer_offset = hud->whitelines.vbuf.buffer_offset +
721 hud->whitelines.buffer_size;
722 hud->text.vertices = hud->whitelines.vertices +
723 hud->whitelines.buffer_size / sizeof(float);
724
725 /* prepare all graphs */
726 hud_batch_query_update(hud->batch_query, pipe);
727
728 LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
729 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
730 gr->query_new_value(gr, pipe);
731 }
732
733 if (pane->sort_items) {
734 LIST_FOR_EACH_ENTRY_SAFE(gr, next, &pane->graph_list, head) {
735 /* ignore the last one */
736 if (&gr->head == pane->graph_list.prev)
737 continue;
738
739 /* This is an incremental bubble sort, because we only do one pass
740 * per frame. It will eventually reach an equilibrium.
741 */
742 if (gr->current_value <
743 list_entry(next, struct hud_graph, head)->current_value) {
744 list_del(&gr->head);
745 list_add(&gr->head, &next->head);
746 }
747 }
748 }
749
750 if (hud->simple)
751 hud_pane_accumulate_vertices_simple(hud, pane);
752 else
753 hud_pane_accumulate_vertices(hud, pane);
754 }
755
756 /* unmap the uploader's vertex buffer before drawing */
757 u_upload_unmap(pipe->stream_uploader);
758 }
759
760 /**
761 * Record queries and draw the HUD. The "cso" parameter acts as a filter.
762 * If "cso" is not the recording context, recording is skipped.
763 * If "cso" is not the drawing context, drawing is skipped.
764 * cso == NULL ignores the filter.
765 */
766 void
hud_run(struct hud_context * hud,struct cso_context * cso,struct pipe_resource * tex)767 hud_run(struct hud_context *hud, struct cso_context *cso,
768 struct pipe_resource *tex)
769 {
770 struct pipe_context *pipe = cso ? cso->pipe : NULL;
771
772 /* If "cso" is the recording or drawing context or NULL, execute
773 * the operation. Otherwise, don't do anything.
774 */
775 if (hud->record_pipe && (!pipe || pipe == hud->record_pipe))
776 hud_stop_queries(hud, hud->record_pipe);
777
778 if (hud->cso && (!cso || cso == hud->cso))
779 hud_draw_results(hud, tex);
780
781 if (hud->record_pipe && (!pipe || pipe == hud->record_pipe))
782 hud_start_queries(hud, hud->record_pipe);
783 }
784
785 /**
786 * Record query results and assemble vertices if "pipe" is a recording but
787 * not drawing context.
788 */
789 void
hud_record_only(struct hud_context * hud,struct pipe_context * pipe)790 hud_record_only(struct hud_context *hud, struct pipe_context *pipe)
791 {
792 assert(pipe);
793
794 /* If it's a drawing context, only hud_run() records query results. */
795 if (pipe == hud->pipe || pipe != hud->record_pipe)
796 return;
797
798 hud_stop_queries(hud, hud->record_pipe);
799 hud_start_queries(hud, hud->record_pipe);
800 }
801
802 static void
fixup_bytes(enum pipe_driver_query_type type,int position,uint64_t * exp10)803 fixup_bytes(enum pipe_driver_query_type type, int position, uint64_t *exp10)
804 {
805 if (type == PIPE_DRIVER_QUERY_TYPE_BYTES && position % 3 == 0)
806 *exp10 = (*exp10 / 1000) * 1024;
807 }
808
809 /**
810 * Set the maximum value for the Y axis of the graph.
811 * This scales the graph accordingly.
812 */
813 void
hud_pane_set_max_value(struct hud_pane * pane,uint64_t value)814 hud_pane_set_max_value(struct hud_pane *pane, uint64_t value)
815 {
816 double leftmost_digit;
817 uint64_t exp10;
818 int i;
819
820 /* The following code determines the max_value in the graph as well as
821 * how many describing lines are drawn. The max_value is rounded up,
822 * so that all drawn numbers are rounded for readability.
823 * We want to print multiples of a simple number instead of multiples of
824 * hard-to-read numbers like 1.753.
825 */
826
827 /* Find the left-most digit. Make sure exp10 * 10 and fixup_bytes doesn't
828 * overflow. (11 is safe) */
829 exp10 = 1;
830 for (i = 0; exp10 <= UINT64_MAX / 11 && exp10 * 9 < value; i++) {
831 exp10 *= 10;
832 fixup_bytes(pane->type, i + 1, &exp10);
833 }
834
835 leftmost_digit = DIV_ROUND_UP(value, exp10);
836
837 /* Round 9 to 10. */
838 if (leftmost_digit == 9) {
839 leftmost_digit = 1;
840 exp10 *= 10;
841 fixup_bytes(pane->type, i + 1, &exp10);
842 }
843
844 switch ((unsigned)leftmost_digit) {
845 case 1:
846 pane->last_line = 5; /* lines in +1/5 increments */
847 break;
848 case 2:
849 pane->last_line = 8; /* lines in +1/4 increments. */
850 break;
851 case 3:
852 case 4:
853 pane->last_line = leftmost_digit * 2; /* lines in +1/2 increments */
854 break;
855 case 5:
856 case 6:
857 case 7:
858 case 8:
859 pane->last_line = leftmost_digit; /* lines in +1 increments */
860 break;
861 default:
862 assert(0);
863 }
864
865 /* Truncate {3,4} to {2.5, 3.5} if possible. */
866 for (i = 3; i <= 4; i++) {
867 if (leftmost_digit == i && value <= (i - 0.5) * exp10) {
868 leftmost_digit = i - 0.5;
869 pane->last_line = leftmost_digit * 2; /* lines in +1/2 increments. */
870 }
871 }
872
873 /* Truncate 2 to a multiple of 0.2 in (1, 1.6] if possible. */
874 if (leftmost_digit == 2) {
875 for (i = 1; i <= 3; i++) {
876 if (value <= (1 + i*0.2) * exp10) {
877 leftmost_digit = 1 + i*0.2;
878 pane->last_line = 5 + i; /* lines in +1/5 increments. */
879 break;
880 }
881 }
882 }
883
884 pane->max_value = leftmost_digit * exp10;
885 pane->yscale = -(int)pane->inner_height / (float)pane->max_value;
886 }
887
888 static void
hud_pane_update_dyn_ceiling(struct hud_graph * gr,struct hud_pane * pane)889 hud_pane_update_dyn_ceiling(struct hud_graph *gr, struct hud_pane *pane)
890 {
891 unsigned i;
892 float tmp = 0.0f;
893
894 if (pane->dyn_ceil_last_ran != gr->index) {
895 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
896 for (i = 0; i < gr->num_vertices; ++i) {
897 tmp = gr->vertices[i * 2 + 1] > tmp ?
898 gr->vertices[i * 2 + 1] : tmp;
899 }
900 }
901
902 /* Avoid setting it lower than the initial starting height. */
903 tmp = tmp > pane->initial_max_value ? tmp : pane->initial_max_value;
904 hud_pane_set_max_value(pane, tmp);
905 }
906
907 /*
908 * Mark this adjustment run so we could avoid repeating a full update
909 * again needlessly in case the pane has more than one graph.
910 */
911 pane->dyn_ceil_last_ran = gr->index;
912 }
913
914 static struct hud_pane *
hud_pane_create(struct hud_context * hud,unsigned x1,unsigned y1,unsigned x2,unsigned y2,unsigned y_simple,unsigned period,uint64_t max_value,uint64_t ceiling,bool dyn_ceiling,bool sort_items)915 hud_pane_create(struct hud_context *hud,
916 unsigned x1, unsigned y1, unsigned x2, unsigned y2,
917 unsigned y_simple,
918 unsigned period, uint64_t max_value, uint64_t ceiling,
919 bool dyn_ceiling, bool sort_items)
920 {
921 struct hud_pane *pane = CALLOC_STRUCT(hud_pane);
922
923 if (!pane)
924 return NULL;
925
926 pane->hud = hud;
927 pane->x1 = x1;
928 pane->y1 = y1;
929 pane->x2 = x2;
930 pane->y2 = y2;
931 pane->y_simple = y_simple;
932 pane->inner_x1 = x1 + 1;
933 pane->inner_x2 = x2 - 1;
934 pane->inner_y1 = y1 + 1;
935 pane->inner_y2 = y2 - 1;
936 pane->inner_width = pane->inner_x2 - pane->inner_x1;
937 pane->inner_height = pane->inner_y2 - pane->inner_y1;
938 pane->period = period;
939 pane->max_num_vertices = (x2 - x1 + 2) / 2;
940 pane->ceiling = ceiling;
941 pane->dyn_ceiling = dyn_ceiling;
942 pane->dyn_ceil_last_ran = 0;
943 pane->sort_items = sort_items;
944 pane->initial_max_value = max_value;
945 hud_pane_set_max_value(pane, max_value);
946 list_inithead(&pane->graph_list);
947 return pane;
948 }
949
950 /* replace '-' with a space */
951 static void
strip_hyphens(char * s)952 strip_hyphens(char *s)
953 {
954 while (*s) {
955 if (*s == '-')
956 *s = ' ';
957 s++;
958 }
959 }
960
961 /**
962 * Add a graph to an existing pane.
963 * One pane can contain multiple graphs over each other.
964 */
965 void
hud_pane_add_graph(struct hud_pane * pane,struct hud_graph * gr)966 hud_pane_add_graph(struct hud_pane *pane, struct hud_graph *gr)
967 {
968 static const float colors[][3] = {
969 {0, 1, 0},
970 {1, 0, 0},
971 {0, 1, 1},
972 {1, 0, 1},
973 {1, 1, 0},
974 {0.5, 1, 0.5},
975 {1, 0.5, 0.5},
976 {0.5, 1, 1},
977 {1, 0.5, 1},
978 {1, 1, 0.5},
979 {0, 0.5, 0},
980 {0.5, 0, 0},
981 {0, 0.5, 0.5},
982 {0.5, 0, 0.5},
983 {0.5, 0.5, 0},
984 };
985 unsigned color = pane->next_color % ARRAY_SIZE(colors);
986
987 strip_hyphens(gr->name);
988
989 gr->vertices = MALLOC(pane->max_num_vertices * sizeof(float) * 2);
990 gr->color[0] = colors[color][0];
991 gr->color[1] = colors[color][1];
992 gr->color[2] = colors[color][2];
993 gr->pane = pane;
994 list_addtail(&gr->head, &pane->graph_list);
995 pane->num_graphs++;
996 pane->next_color++;
997 }
998
999 void
hud_graph_add_value(struct hud_graph * gr,double value)1000 hud_graph_add_value(struct hud_graph *gr, double value)
1001 {
1002 gr->current_value = value;
1003 value = value > gr->pane->ceiling ? gr->pane->ceiling : value;
1004
1005 if (gr->fd) {
1006 if (gr->fd == stdout && !gr->separator) {
1007 fprintf(gr->fd, "%s: ", gr->name);
1008 }
1009 if (fabs(value - lround(value)) > FLT_EPSILON) {
1010 fprintf(gr->fd, get_float_modifier(value), value);
1011 }
1012 else {
1013 fprintf(gr->fd, "%" PRIu64, (uint64_t) lround(value));
1014 }
1015 fprintf(gr->fd, "%s", gr->separator ? gr->separator : "\n");
1016 }
1017
1018 if (gr->index == gr->pane->max_num_vertices) {
1019 gr->vertices[0] = 0;
1020 gr->vertices[1] = gr->vertices[(gr->index-1)*2+1];
1021 gr->index = 1;
1022 }
1023 gr->vertices[(gr->index)*2+0] = (float) (gr->index * 2);
1024 gr->vertices[(gr->index)*2+1] = (float) value;
1025 gr->index++;
1026
1027 if (gr->num_vertices < gr->pane->max_num_vertices) {
1028 gr->num_vertices++;
1029 }
1030
1031 if (gr->pane->dyn_ceiling == true) {
1032 hud_pane_update_dyn_ceiling(gr, gr->pane);
1033 }
1034 if (value > gr->pane->max_value) {
1035 hud_pane_set_max_value(gr->pane, value);
1036 }
1037 }
1038
1039 static void
hud_graph_destroy(struct hud_graph * graph,struct pipe_context * pipe)1040 hud_graph_destroy(struct hud_graph *graph, struct pipe_context *pipe)
1041 {
1042 FREE(graph->vertices);
1043 if (graph->free_query_data)
1044 graph->free_query_data(graph->query_data, pipe);
1045 if (graph->fd)
1046 fclose(graph->fd);
1047 FREE(graph);
1048 }
1049
strcat_without_spaces(char * dst,const char * src)1050 static void strcat_without_spaces(char *dst, const char *src)
1051 {
1052 dst += strlen(dst);
1053 while (*src) {
1054 if (*src == ' ')
1055 *dst++ = '_';
1056 else
1057 *dst++ = *src;
1058 src++;
1059 }
1060 *dst = 0;
1061 }
1062
1063
1064 #if DETECT_OS_WINDOWS
1065
1066 #define PATH_SEP "\\"
1067
1068 #else
1069
1070 #define PATH_SEP "/"
1071
1072 #endif
1073
1074
1075 /**
1076 * If the GALLIUM_HUD_DUMP_DIR env var is set, we'll write the raw
1077 * HUD values to files at ${GALLIUM_HUD_DUMP_DIR}/<stat> where <stat>
1078 * is a HUD variable such as "fps", or "cpu"
1079 */
1080 static void
hud_graph_set_dump_file(struct hud_graph * gr,const char * hud_dump_dir,bool to_stdout,const char * separator)1081 hud_graph_set_dump_file(struct hud_graph *gr, const char *hud_dump_dir,
1082 bool to_stdout, const char *separator)
1083 {
1084 if (hud_dump_dir) {
1085 char *dump_file = malloc(strlen(hud_dump_dir) + sizeof(PATH_SEP)
1086 + sizeof(gr->name));
1087 if (dump_file) {
1088 strcpy(dump_file, hud_dump_dir);
1089 strcat(dump_file, PATH_SEP);
1090 strcat_without_spaces(dump_file, gr->name);
1091 gr->fd = fopen(dump_file, "a+");
1092 free(dump_file);
1093 }
1094 } else if (to_stdout) {
1095 gr->fd = stdout;
1096 }
1097
1098 if (gr->fd) {
1099 /* flush output after each line is written */
1100 setvbuf(gr->fd, NULL, _IOLBF, 0);
1101 }
1102
1103 gr->separator = separator;
1104 }
1105
1106 /**
1107 * Read a string from the environment variable.
1108 * The separators "+", ",", ":", and ";" terminate the string.
1109 * Return the number of read characters.
1110 */
1111 static int
parse_string(const char * s,char * out)1112 parse_string(const char *s, char *out)
1113 {
1114 int i;
1115
1116 for (i = 0; *s && *s != '+' && *s != ',' && *s != ':' && *s != ';' && *s != '=';
1117 s++, out++, i++)
1118 *out = *s;
1119
1120 *out = 0;
1121
1122 if (*s && !i) {
1123 fprintf(stderr, "gallium_hud: syntax error: unexpected '%c' (%i) while "
1124 "parsing a string\n", *s, *s);
1125 fflush(stderr);
1126 }
1127
1128 return i;
1129 }
1130
1131 static char *
read_pane_settings(char * str,unsigned * const x,unsigned * const y,unsigned * const width,unsigned * const height,uint64_t * const ceiling,bool * const dyn_ceiling,bool * reset_colors,bool * sort_items)1132 read_pane_settings(char *str, unsigned * const x, unsigned * const y,
1133 unsigned * const width, unsigned * const height,
1134 uint64_t * const ceiling, bool * const dyn_ceiling,
1135 bool *reset_colors, bool *sort_items)
1136 {
1137 char *ret = str;
1138 unsigned tmp;
1139
1140 while (*str == '.') {
1141 ++str;
1142 switch (*str) {
1143 case 'x':
1144 ++str;
1145 *x = strtoul(str, &ret, 10);
1146 str = ret;
1147 break;
1148
1149 case 'y':
1150 ++str;
1151 *y = strtoul(str, &ret, 10);
1152 str = ret;
1153 break;
1154
1155 case 'w':
1156 ++str;
1157 tmp = strtoul(str, &ret, 10);
1158 *width = tmp > 80 ? tmp : 80; /* 80 is chosen arbitrarily */
1159 str = ret;
1160 break;
1161
1162 /*
1163 * Prevent setting height to less than 50. If the height is set to less,
1164 * the text of the Y axis labels on the graph will start overlapping.
1165 */
1166 case 'h':
1167 ++str;
1168 tmp = strtoul(str, &ret, 10);
1169 *height = tmp > 50 ? tmp : 50;
1170 str = ret;
1171 break;
1172
1173 case 'c':
1174 ++str;
1175 tmp = strtoul(str, &ret, 10);
1176 *ceiling = tmp > 10 ? tmp : 10;
1177 str = ret;
1178 break;
1179
1180 case 'd':
1181 ++str;
1182 ret = str;
1183 *dyn_ceiling = true;
1184 break;
1185
1186 case 'r':
1187 ++str;
1188 ret = str;
1189 *reset_colors = true;
1190 break;
1191
1192 case 's':
1193 ++str;
1194 ret = str;
1195 *sort_items = true;
1196 break;
1197
1198 default:
1199 fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *str);
1200 fflush(stderr);
1201 }
1202
1203 }
1204
1205 return ret;
1206 }
1207
1208 static bool
has_occlusion_query(struct pipe_screen * screen)1209 has_occlusion_query(struct pipe_screen *screen)
1210 {
1211 return screen->get_param(screen, PIPE_CAP_OCCLUSION_QUERY) != 0;
1212 }
1213
1214 static bool
has_streamout(struct pipe_screen * screen)1215 has_streamout(struct pipe_screen *screen)
1216 {
1217 return screen->get_param(screen, PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
1218 }
1219
1220 static bool
has_pipeline_stats_query(struct pipe_screen * screen)1221 has_pipeline_stats_query(struct pipe_screen *screen)
1222 {
1223 return screen->get_param(screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS) != 0;
1224 }
1225
1226 static void
hud_parse_env_var(struct hud_context * hud,struct pipe_screen * screen,const char * env,unsigned period_ms)1227 hud_parse_env_var(struct hud_context *hud, struct pipe_screen *screen,
1228 const char *env, unsigned period_ms)
1229 {
1230 unsigned num, i;
1231 char name_a[256], s[256];
1232 char *name;
1233 struct hud_pane *pane = NULL;
1234 unsigned x = 10, y = 10, y_simple = 10;
1235 unsigned width = 251, height = 100;
1236 unsigned period = period_ms * 1000;
1237 uint64_t ceiling = UINT64_MAX;
1238 unsigned column_width = 251;
1239 bool dyn_ceiling = false;
1240 bool reset_colors = false;
1241 bool sort_items = false;
1242 bool is_csv = false;
1243 bool to_stdout = false;
1244 const char *period_env;
1245
1246 if (strncmp(env, "simple,", 7) == 0) {
1247 hud->simple = true;
1248 env += 7;
1249 }
1250
1251 /*
1252 * The GALLIUM_HUD_PERIOD env var sets the graph update rate.
1253 * The env var is in seconds (a float).
1254 * Zero means update after every frame.
1255 */
1256 period_env = os_get_option("GALLIUM_HUD_PERIOD");
1257 if (period_env) {
1258 float p = (float) atof(period_env);
1259 if (p >= 0.0f) {
1260 period = (unsigned) (p * 1000 * 1000);
1261 }
1262 }
1263
1264 while ((num = parse_string(env, name_a)) != 0) {
1265 bool added = true;
1266
1267 env += num;
1268
1269 /* check for explicit location, size and etc. settings */
1270 name = read_pane_settings(name_a, &x, &y, &width, &height, &ceiling,
1271 &dyn_ceiling, &reset_colors, &sort_items);
1272
1273 /*
1274 * Keep track of overall column width to avoid pane overlapping in case
1275 * later we create a new column while the bottom pane in the current
1276 * column is less wide than the rest of the panes in it.
1277 */
1278 column_width = width > column_width ? width : column_width;
1279
1280 if (!pane) {
1281 pane = hud_pane_create(hud, x, y, x + width, y + height, y_simple,
1282 period, 10, ceiling, dyn_ceiling, sort_items);
1283 if (!pane)
1284 return;
1285 }
1286
1287 if (reset_colors) {
1288 pane->next_color = 0;
1289 reset_colors = false;
1290 }
1291
1292 /* Add a graph. */
1293 #if defined(HAVE_GALLIUM_EXTRA_HUD) || defined(HAVE_LIBSENSORS)
1294 char arg_name[64];
1295 #endif
1296 /* IF YOU CHANGE THIS, UPDATE print_help! */
1297 if (strcmp(name, "fps") == 0) {
1298 hud_fps_graph_install(pane);
1299 }
1300 else if (strcmp(name, "frametime") == 0) {
1301 hud_frametime_graph_install(pane);
1302 }
1303 else if (strcmp(name, "cpu") == 0) {
1304 hud_cpu_graph_install(pane, ALL_CPUS);
1305 }
1306 else if (sscanf(name, "cpu%u%s", &i, s) == 1) {
1307 hud_cpu_graph_install(pane, i);
1308 }
1309 else if (strcmp(name, "API-thread-busy") == 0) {
1310 hud_thread_busy_install(pane, name, false);
1311 }
1312 else if (strcmp(name, "API-thread-offloaded-slots") == 0) {
1313 hud_thread_counter_install(pane, name, HUD_COUNTER_OFFLOADED);
1314 }
1315 else if (strcmp(name, "API-thread-direct-slots") == 0) {
1316 hud_thread_counter_install(pane, name, HUD_COUNTER_DIRECT);
1317 }
1318 else if (strcmp(name, "API-thread-num-syncs") == 0) {
1319 hud_thread_counter_install(pane, name, HUD_COUNTER_SYNCS);
1320 }
1321 else if (strcmp(name, "API-thread-num-batches") == 0) {
1322 hud_thread_counter_install(pane, name, HUD_COUNTER_BATCHES);
1323 }
1324 else if (strcmp(name, "main-thread-busy") == 0) {
1325 hud_thread_busy_install(pane, name, true);
1326 }
1327 #ifdef HAVE_GALLIUM_EXTRA_HUD
1328 else if (sscanf(name, "nic-rx-%s", arg_name) == 1) {
1329 hud_nic_graph_install(pane, arg_name, NIC_DIRECTION_RX);
1330 }
1331 else if (sscanf(name, "nic-tx-%s", arg_name) == 1) {
1332 hud_nic_graph_install(pane, arg_name, NIC_DIRECTION_TX);
1333 }
1334 else if (sscanf(name, "nic-rssi-%s", arg_name) == 1) {
1335 hud_nic_graph_install(pane, arg_name, NIC_RSSI_DBM);
1336 pane->type = PIPE_DRIVER_QUERY_TYPE_DBM;
1337 }
1338 else if (sscanf(name, "diskstat-rd-%s", arg_name) == 1) {
1339 hud_diskstat_graph_install(pane, arg_name, DISKSTAT_RD);
1340 pane->type = PIPE_DRIVER_QUERY_TYPE_BYTES;
1341 }
1342 else if (sscanf(name, "diskstat-wr-%s", arg_name) == 1) {
1343 hud_diskstat_graph_install(pane, arg_name, DISKSTAT_WR);
1344 pane->type = PIPE_DRIVER_QUERY_TYPE_BYTES;
1345 }
1346 else if (sscanf(name, "cpufreq-min-cpu%u", &i) == 1) {
1347 hud_cpufreq_graph_install(pane, i, CPUFREQ_MINIMUM);
1348 pane->type = PIPE_DRIVER_QUERY_TYPE_HZ;
1349 }
1350 else if (sscanf(name, "cpufreq-cur-cpu%u", &i) == 1) {
1351 hud_cpufreq_graph_install(pane, i, CPUFREQ_CURRENT);
1352 pane->type = PIPE_DRIVER_QUERY_TYPE_HZ;
1353 }
1354 else if (sscanf(name, "cpufreq-max-cpu%u", &i) == 1) {
1355 hud_cpufreq_graph_install(pane, i, CPUFREQ_MAXIMUM);
1356 pane->type = PIPE_DRIVER_QUERY_TYPE_HZ;
1357 }
1358 #endif
1359 #ifdef HAVE_LIBSENSORS
1360 else if (sscanf(name, "sensors_temp_cu-%s", arg_name) == 1) {
1361 hud_sensors_temp_graph_install(pane, arg_name,
1362 SENSORS_TEMP_CURRENT);
1363 pane->type = PIPE_DRIVER_QUERY_TYPE_TEMPERATURE;
1364 }
1365 else if (sscanf(name, "sensors_temp_cr-%s", arg_name) == 1) {
1366 hud_sensors_temp_graph_install(pane, arg_name,
1367 SENSORS_TEMP_CRITICAL);
1368 pane->type = PIPE_DRIVER_QUERY_TYPE_TEMPERATURE;
1369 }
1370 else if (sscanf(name, "sensors_volt_cu-%s", arg_name) == 1) {
1371 hud_sensors_temp_graph_install(pane, arg_name,
1372 SENSORS_VOLTAGE_CURRENT);
1373 pane->type = PIPE_DRIVER_QUERY_TYPE_VOLTS;
1374 }
1375 else if (sscanf(name, "sensors_curr_cu-%s", arg_name) == 1) {
1376 hud_sensors_temp_graph_install(pane, arg_name,
1377 SENSORS_CURRENT_CURRENT);
1378 pane->type = PIPE_DRIVER_QUERY_TYPE_AMPS;
1379 }
1380 else if (sscanf(name, "sensors_pow_cu-%s", arg_name) == 1) {
1381 hud_sensors_temp_graph_install(pane, arg_name,
1382 SENSORS_POWER_CURRENT);
1383 pane->type = PIPE_DRIVER_QUERY_TYPE_WATTS;
1384 }
1385 #endif
1386 else if (strcmp(name, "samples-passed") == 0 &&
1387 has_occlusion_query(screen)) {
1388 hud_pipe_query_install(&hud->batch_query, pane,
1389 "samples-passed",
1390 PIPE_QUERY_OCCLUSION_COUNTER, 0, 0,
1391 PIPE_DRIVER_QUERY_TYPE_UINT64,
1392 PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE,
1393 0);
1394 }
1395 else if (strcmp(name, "primitives-generated") == 0 &&
1396 has_streamout(screen)) {
1397 hud_pipe_query_install(&hud->batch_query, pane,
1398 "primitives-generated",
1399 PIPE_QUERY_PRIMITIVES_GENERATED, 0, 0,
1400 PIPE_DRIVER_QUERY_TYPE_UINT64,
1401 PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE,
1402 0);
1403 }
1404 else if (strcmp(name, "stdout") == 0) {
1405 to_stdout = true;
1406 }
1407 else if (strcmp(name, "csv") == 0) {
1408 to_stdout = true;
1409 is_csv = true;
1410 }
1411 else {
1412 bool processed = false;
1413
1414 /* pipeline statistics queries */
1415 if (has_pipeline_stats_query(screen)) {
1416 static const char *pipeline_statistics_names[] =
1417 {
1418 "ia-vertices",
1419 "ia-primitives",
1420 "vs-invocations",
1421 "gs-invocations",
1422 "gs-primitives",
1423 "clipper-invocations",
1424 "clipper-primitives-generated",
1425 "ps-invocations",
1426 "hs-invocations",
1427 "ds-invocations",
1428 "cs-invocations"
1429 };
1430 for (i = 0; i < ARRAY_SIZE(pipeline_statistics_names); ++i)
1431 if (strcmp(name, pipeline_statistics_names[i]) == 0)
1432 break;
1433 if (i < ARRAY_SIZE(pipeline_statistics_names)) {
1434 hud_pipe_query_install(&hud->batch_query, pane, name,
1435 PIPE_QUERY_PIPELINE_STATISTICS, i,
1436 0, PIPE_DRIVER_QUERY_TYPE_UINT64,
1437 PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE,
1438 0);
1439 processed = true;
1440 }
1441 }
1442
1443 /* driver queries */
1444 if (!processed) {
1445 if (!hud_driver_query_install(&hud->batch_query, pane,
1446 screen, name)) {
1447 fprintf(stderr, "gallium_hud: unknown driver query '%s'\n", name);
1448 fflush(stderr);
1449 added = false;
1450 }
1451 }
1452 }
1453
1454 if (*env == ':') {
1455 env++;
1456
1457 if (!pane) {
1458 fprintf(stderr, "gallium_hud: syntax error: unexpected ':', "
1459 "expected a name\n");
1460 fflush(stderr);
1461 break;
1462 }
1463
1464 num = parse_string(env, s);
1465 env += num;
1466
1467 if (num && sscanf(s, "%u", &i) == 1) {
1468 hud_pane_set_max_value(pane, i);
1469 pane->initial_max_value = i;
1470 }
1471 else {
1472 fprintf(stderr, "gallium_hud: syntax error: unexpected '%c' (%i) "
1473 "after ':'\n", *env, *env);
1474 fflush(stderr);
1475 }
1476 }
1477
1478 if (*env == '=') {
1479 env++;
1480
1481 if (!pane) {
1482 fprintf(stderr, "gallium_hud: syntax error: unexpected '=', "
1483 "expected a name\n");
1484 fflush(stderr);
1485 break;
1486 }
1487
1488 num = parse_string(env, s);
1489 env += num;
1490
1491 strip_hyphens(s);
1492 if (added && !list_is_empty(&pane->graph_list)) {
1493 struct hud_graph *graph;
1494 graph = list_entry(pane->graph_list.prev, struct hud_graph, head);
1495 snprintf(graph->name, sizeof(graph->name), "%s", s);
1496 }
1497 }
1498
1499 if (*env == 0)
1500 break;
1501
1502 /* parse a separator */
1503 switch (*env) {
1504 case '+':
1505 env++;
1506 break;
1507
1508 case ',':
1509 env++;
1510 if (!pane)
1511 break;
1512
1513 y += height + hud->font.glyph_height * (pane->num_graphs + 2);
1514 y_simple += hud->font.glyph_height * (pane->num_graphs + 1);
1515 height = 100;
1516
1517 if (pane && pane->num_graphs) {
1518 list_addtail(&pane->head, &hud->pane_list);
1519 pane = NULL;
1520 }
1521 break;
1522
1523 case ';':
1524 env++;
1525 y = 10;
1526 y_simple = 10;
1527 x += column_width + hud->font.glyph_width * 9;
1528 height = 100;
1529
1530 if (pane && pane->num_graphs) {
1531 list_addtail(&pane->head, &hud->pane_list);
1532 pane = NULL;
1533 }
1534
1535 /* Starting a new column; reset column width. */
1536 column_width = 251;
1537 break;
1538
1539 default:
1540 fprintf(stderr, "gallium_hud: syntax error: unexpected '%c'\n", *env);
1541 fflush(stderr);
1542 }
1543
1544 /* Reset to defaults for the next pane in case these were modified. */
1545 width = 251;
1546 ceiling = UINT64_MAX;
1547 dyn_ceiling = false;
1548 sort_items = false;
1549
1550 }
1551
1552 if (pane) {
1553 if (pane->num_graphs) {
1554 list_addtail(&pane->head, &hud->pane_list);
1555 }
1556 else {
1557 FREE(pane);
1558 }
1559 }
1560
1561 const char *hud_dump_dir = os_get_option("GALLIUM_HUD_DUMP_DIR");
1562 if ((hud_dump_dir && access(hud_dump_dir, W_OK) == 0) || to_stdout) {
1563 LIST_FOR_EACH_ENTRY(pane, &hud->pane_list, head) {
1564 struct hud_graph *gr;
1565
1566 LIST_FOR_EACH_ENTRY(gr, &pane->graph_list, head) {
1567 char *separator = NULL;
1568
1569 if (is_csv) {
1570 if (gr ==
1571 list_last_entry(&pane->graph_list, struct hud_graph, head))
1572 separator = "\n";
1573 else
1574 separator = ", ";
1575 }
1576 hud_graph_set_dump_file(gr, hud_dump_dir, to_stdout, separator);
1577 }
1578 }
1579 }
1580 }
1581
1582 static void
print_help(struct pipe_screen * screen)1583 print_help(struct pipe_screen *screen)
1584 {
1585 int i, num_queries, num_cpus = hud_get_num_cpus();
1586
1587 puts("Syntax: GALLIUM_HUD=name1[+name2][...][:value1][,nameI...][;nameJ...]");
1588 puts("");
1589 puts(" Names are identifiers of data sources which will be drawn as graphs");
1590 puts(" in panes. Multiple graphs can be drawn in the same pane.");
1591 puts(" There can be multiple panes placed in rows and columns.");
1592 puts("");
1593 puts(" '+' separates names which will share a pane.");
1594 puts(" ':[value]' specifies the initial maximum value of the Y axis");
1595 puts(" for the given pane.");
1596 puts(" ',' creates a new pane below the last one.");
1597 puts(" ';' creates a new pane at the top of the next column.");
1598 puts(" '=' followed by a string, changes the name of the last data source");
1599 puts(" to that string");
1600 puts("");
1601 puts(" Example: GALLIUM_HUD=\"cpu,fps;primitives-generated\"");
1602 puts("");
1603 puts(" Additionally, by prepending '.[identifier][value]' modifiers to");
1604 puts(" a name, it is possible to explicitly set the location and size");
1605 puts(" of a pane, along with limiting overall maximum value of the");
1606 puts(" Y axis and activating dynamic readjustment of the Y axis.");
1607 puts(" Several modifiers may be applied to the same pane simultaneously.");
1608 puts("");
1609 puts(" 'x[value]' sets the location of the pane on the x axis relative");
1610 puts(" to the upper-left corner of the viewport, in pixels.");
1611 puts(" 'y[value]' sets the location of the pane on the y axis relative");
1612 puts(" to the upper-left corner of the viewport, in pixels.");
1613 puts(" 'w[value]' sets width of the graph pixels.");
1614 puts(" 'h[value]' sets height of the graph in pixels.");
1615 puts(" 'c[value]' sets the ceiling of the value of the Y axis.");
1616 puts(" If the graph needs to draw values higher than");
1617 puts(" the ceiling allows, the value is clamped.");
1618 puts(" 'd' activates dynamic Y axis readjustment to set the value of");
1619 puts(" the Y axis to match the highest value still visible in the graph.");
1620 puts(" 'r' resets the color counter (the next color will be green)");
1621 puts(" 's' sort items below graphs in descending order");
1622 puts("");
1623 puts(" If 'c' and 'd' modifiers are used simultaneously, both are in effect:");
1624 puts(" the Y axis does not go above the restriction imposed by 'c' while");
1625 puts(" still adjusting the value of the Y axis down when appropriate.");
1626 puts("");
1627 puts(" You can change behavior of the whole HUD by adding these options at");
1628 puts(" the beginning of the environment variable:");
1629 puts(" 'simple,' disables all the fancy stuff and only draws text.");
1630 puts("");
1631 puts(" Example: GALLIUM_HUD=\".w256.h64.x1600.y520.d.c1000fps+cpu,.datom-count\"");
1632 puts("");
1633 puts(" Available names:");
1634 puts(" stdout (prints the counters value to stdout)");
1635 puts(" csv (prints the counter values to stdout as CSV, use + to separate names)");
1636 puts(" fps");
1637 puts(" frametime");
1638 puts(" cpu");
1639
1640 for (i = 0; i < num_cpus; i++)
1641 printf(" cpu%i\n", i);
1642
1643 if (has_occlusion_query(screen))
1644 puts(" samples-passed");
1645 if (has_streamout(screen))
1646 puts(" primitives-generated");
1647
1648 if (has_pipeline_stats_query(screen)) {
1649 puts(" ia-vertices");
1650 puts(" ia-primitives");
1651 puts(" vs-invocations");
1652 puts(" gs-invocations");
1653 puts(" gs-primitives");
1654 puts(" clipper-invocations");
1655 puts(" clipper-primitives-generated");
1656 puts(" ps-invocations");
1657 puts(" hs-invocations");
1658 puts(" ds-invocations");
1659 puts(" cs-invocations");
1660 }
1661
1662 #ifdef HAVE_GALLIUM_EXTRA_HUD
1663 hud_get_num_disks(1);
1664 hud_get_num_nics(1);
1665 hud_get_num_cpufreq(1);
1666 #endif
1667 #ifdef HAVE_LIBSENSORS
1668 hud_get_num_sensors(1);
1669 #endif
1670
1671 if (screen->get_driver_query_info){
1672 bool skipping = false;
1673 struct pipe_driver_query_info info;
1674 num_queries = screen->get_driver_query_info(screen, 0, NULL);
1675
1676 for (i = 0; i < num_queries; i++){
1677 screen->get_driver_query_info(screen, i, &info);
1678 if (info.flags & PIPE_DRIVER_QUERY_FLAG_DONT_LIST) {
1679 if (!skipping)
1680 puts(" ...");
1681 skipping = true;
1682 } else {
1683 printf(" %s\n", info.name);
1684 skipping = false;
1685 }
1686 }
1687 }
1688
1689 puts("");
1690 fflush(stdout);
1691 }
1692
1693 static void
hud_unset_draw_context(struct hud_context * hud)1694 hud_unset_draw_context(struct hud_context *hud)
1695 {
1696 struct pipe_context *pipe = hud->pipe;
1697
1698 if (!pipe)
1699 return;
1700
1701 pipe_sampler_view_reference(&hud->font_sampler_view, NULL);
1702
1703 if (hud->fs_color) {
1704 pipe->delete_fs_state(pipe, hud->fs_color);
1705 hud->fs_color = NULL;
1706 }
1707 if (hud->fs_text) {
1708 pipe->delete_fs_state(pipe, hud->fs_text);
1709 hud->fs_text = NULL;
1710 }
1711 if (hud->vs_color) {
1712 pipe->delete_vs_state(pipe, hud->vs_color);
1713 hud->vs_color = NULL;
1714 }
1715 if (hud->vs_text) {
1716 pipe->delete_vs_state(pipe, hud->vs_text);
1717 hud->vs_text = NULL;
1718 }
1719
1720 hud->cso = NULL;
1721 hud->pipe = NULL;
1722 }
1723
1724 static bool
hud_set_draw_context(struct hud_context * hud,struct cso_context * cso,struct st_context * st,hud_st_invalidate_state_func st_invalidate_state)1725 hud_set_draw_context(struct hud_context *hud, struct cso_context *cso,
1726 struct st_context *st,
1727 hud_st_invalidate_state_func st_invalidate_state)
1728 {
1729 struct pipe_context *pipe = cso->pipe;
1730
1731 assert(!hud->pipe);
1732 hud->pipe = pipe;
1733 hud->cso = cso;
1734 hud->st = st;
1735 hud->st_invalidate_state = st_invalidate_state;
1736
1737 struct pipe_sampler_view view_templ;
1738 u_sampler_view_default_template(
1739 &view_templ, hud->font.texture, hud->font.texture->format);
1740 hud->font_sampler_view = pipe->create_sampler_view(pipe, hud->font.texture,
1741 &view_templ);
1742 if (!hud->font_sampler_view)
1743 goto fail;
1744
1745 /* color fragment shader */
1746 hud->fs_color =
1747 util_make_fragment_passthrough_shader(pipe,
1748 TGSI_SEMANTIC_COLOR,
1749 TGSI_INTERPOLATE_CONSTANT,
1750 true);
1751
1752 /* text fragment shader */
1753 {
1754 /* Read a texture and do .xxxx swizzling. */
1755 static const char *fragment_shader_text = {
1756 "FRAG\n"
1757 "DCL IN[0], GENERIC[0], LINEAR\n"
1758 "DCL SAMP[0]\n"
1759 "DCL SVIEW[0], 2D, FLOAT\n"
1760 "DCL OUT[0], COLOR[0]\n"
1761 "DCL TEMP[0]\n"
1762
1763 "TEX TEMP[0], IN[0], SAMP[0], 2D\n"
1764 "MOV OUT[0], TEMP[0].xxxx\n"
1765 "END\n"
1766 };
1767
1768 struct tgsi_token tokens[1000];
1769 struct pipe_shader_state state = {0};
1770
1771 if (!tgsi_text_translate(fragment_shader_text, tokens, ARRAY_SIZE(tokens))) {
1772 assert(0);
1773 goto fail;
1774 }
1775 pipe_shader_state_from_tgsi(&state, tokens);
1776 hud->fs_text = pipe->create_fs_state(pipe, &state);
1777 }
1778
1779 /* color vertex shader */
1780 {
1781 static const char *vertex_shader_text = {
1782 "VERT\n"
1783 "DCL IN[0..1]\n"
1784 "DCL OUT[0], POSITION\n"
1785 "DCL OUT[1], COLOR[0]\n" /* color */
1786 "DCL OUT[2], GENERIC[0]\n" /* texcoord */
1787 /* [0] = color,
1788 * [1] = (2/fb_width, 2/fb_height, xoffset, yoffset)
1789 * [2] = (xscale, yscale, 0, 0)
1790 * [3] = rotation_matrix */
1791 "DCL CONST[0][0..3]\n"
1792 "DCL TEMP[0..2]\n"
1793 "IMM[0] FLT32 { -1, 0, 0, 1 }\n"
1794
1795 /* v = in * (xscale, yscale) + (xoffset, yoffset) */
1796 "MAD TEMP[0].xy, IN[0], CONST[0][2].xyyy, CONST[0][1].zwww\n"
1797 /* v = v * (2 / fb_width, 2 / fb_height) - (1, 1) */
1798 "MAD TEMP[1].xy, TEMP[0], CONST[0][1].xyyy, IMM[0].xxxx\n"
1799
1800 /* pos = rotation_matrix * v */
1801 "MUL TEMP[2].xyzw, TEMP[1].xyxy, CONST[0][3].xyzw\n"
1802 "ADD OUT[0].xy, TEMP[2].xzzz, TEMP[2].ywww\n"
1803 "MOV OUT[0].zw, IMM[0]\n"
1804
1805 "MOV OUT[1], CONST[0][0]\n"
1806 "MOV OUT[2], IN[1]\n"
1807 "END\n"
1808 };
1809
1810 struct tgsi_token tokens[1000];
1811 struct pipe_shader_state state = {0};
1812 if (!tgsi_text_translate(vertex_shader_text, tokens, ARRAY_SIZE(tokens))) {
1813 assert(0);
1814 goto fail;
1815 }
1816 pipe_shader_state_from_tgsi(&state, tokens);
1817 hud->vs_color = pipe->create_vs_state(pipe, &state);
1818 }
1819
1820 /* text vertex shader */
1821 {
1822 /* similar to the above, without the color component
1823 * to match the varyings in fs_text */
1824 static const char *vertex_shader_text = {
1825 "VERT\n"
1826 "DCL IN[0..1]\n"
1827 "DCL OUT[0], POSITION\n"
1828 "DCL OUT[1], GENERIC[0]\n" /* texcoord */
1829 /* [0] = color,
1830 * [1] = (2/fb_width, 2/fb_height, xoffset, yoffset)
1831 * [2] = (xscale, yscale, 0, 0)
1832 * [3] = rotation_matrix */
1833 "DCL CONST[0][0..3]\n"
1834 "DCL TEMP[0..2]\n"
1835 "IMM[0] FLT32 { -1, 0, 0, 1 }\n"
1836 "IMM[1] FLT32 { 0.0078125, 0.00390625, 1, 1 }\n" // 1.0 / 128, 1.0 / 256, 1, 1
1837
1838 /* v = in * (xscale, yscale) + (xoffset, yoffset) */
1839 "MAD TEMP[0].xy, IN[0], CONST[0][2].xyyy, CONST[0][1].zwww\n"
1840 /* pos = v * (2 / fb_width, 2 / fb_height) - (1, 1) */
1841 "MAD TEMP[1].xy, TEMP[0], CONST[0][1].xyyy, IMM[0].xxxx\n"
1842
1843 /* pos = rotation_matrix * v */
1844 "MUL TEMP[2].xyzw, TEMP[1].xyxy, CONST[0][3].xyzw\n"
1845 "ADD OUT[0].xy, TEMP[2].xzzz, TEMP[2].ywww\n"
1846 "MOV OUT[0].zw, IMM[0]\n"
1847
1848 "MUL OUT[1], IN[1], IMM[1]\n"
1849 "END\n"
1850 };
1851
1852 struct tgsi_token tokens[1000];
1853 struct pipe_shader_state state = {0};
1854 if (!tgsi_text_translate(vertex_shader_text, tokens, ARRAY_SIZE(tokens))) {
1855 assert(0);
1856 goto fail;
1857 }
1858 pipe_shader_state_from_tgsi(&state, tokens);
1859 hud->vs_text = pipe->create_vs_state(pipe, &state);
1860 }
1861
1862 return true;
1863
1864 fail:
1865 hud_unset_draw_context(hud);
1866 fprintf(stderr, "hud: failed to set a draw context");
1867 return false;
1868 }
1869
1870 static void
hud_unset_record_context(struct hud_context * hud)1871 hud_unset_record_context(struct hud_context *hud)
1872 {
1873 struct pipe_context *pipe = hud->record_pipe;
1874 struct hud_pane *pane, *pane_tmp;
1875 struct hud_graph *graph, *graph_tmp;
1876
1877 if (!pipe)
1878 return;
1879
1880 LIST_FOR_EACH_ENTRY_SAFE(pane, pane_tmp, &hud->pane_list, head) {
1881 LIST_FOR_EACH_ENTRY_SAFE(graph, graph_tmp, &pane->graph_list, head) {
1882 list_del(&graph->head);
1883 hud_graph_destroy(graph, pipe);
1884 }
1885 list_del(&pane->head);
1886 FREE(pane);
1887 }
1888
1889 hud_batch_query_cleanup(&hud->batch_query, pipe);
1890 hud->record_pipe = NULL;
1891 }
1892
1893 static void
hud_set_record_context(struct hud_context * hud,struct pipe_context * pipe)1894 hud_set_record_context(struct hud_context *hud, struct pipe_context *pipe)
1895 {
1896 hud->record_pipe = pipe;
1897 }
1898
1899 static void
hud_init_velems(struct cso_velems_state * velems,unsigned stride)1900 hud_init_velems(struct cso_velems_state *velems, unsigned stride)
1901 {
1902 velems->count = 2;
1903 for (unsigned i = 0; i < 2; i++) {
1904 velems->velems[i].src_offset = i * 2 * sizeof(float);
1905 velems->velems[i].src_format = PIPE_FORMAT_R32G32_FLOAT;
1906 velems->velems[i].vertex_buffer_index = 0;
1907 velems->velems[i].src_stride = stride;
1908 }
1909 }
1910
1911 /**
1912 * Create the HUD.
1913 *
1914 * If "share" is non-NULL and GALLIUM_HUD_SHARE=x,y is set, increment the
1915 * reference counter of "share", set "cso" as the recording or drawing context
1916 * according to the environment variable, and return "share".
1917 * This allows sharing the HUD instance within a multi-context share group,
1918 * record queries in one context and draw them in another.
1919 */
1920 struct hud_context *
hud_create(struct cso_context * cso,struct hud_context * share,struct st_context * st,hud_st_invalidate_state_func st_invalidate_state)1921 hud_create(struct cso_context *cso, struct hud_context *share,
1922 struct st_context *st,
1923 hud_st_invalidate_state_func st_invalidate_state)
1924 {
1925 const char *share_env = debug_get_option("GALLIUM_HUD_SHARE", NULL);
1926 unsigned record_ctx = 0, draw_ctx = 0;
1927
1928 if (share_env && sscanf(share_env, "%u,%u", &record_ctx, &draw_ctx) != 2)
1929 share_env = NULL;
1930
1931 if (share && share_env) {
1932 /* All contexts in a share group share the HUD instance.
1933 * Only one context can record queries and only one context
1934 * can draw the HUD.
1935 *
1936 * GALLIUM_HUD_SHARE=x,y determines the context indices.
1937 */
1938 int context_id = p_atomic_inc_return(&share->refcount) - 1;
1939
1940 if (context_id == record_ctx) {
1941 assert(!share->record_pipe);
1942 hud_set_record_context(share, cso->pipe);
1943 }
1944
1945 if (context_id == draw_ctx) {
1946 assert(!share->pipe);
1947 hud_set_draw_context(share, cso, st, st_invalidate_state);
1948 }
1949
1950 return share;
1951 }
1952
1953 struct pipe_screen *screen = cso->pipe->screen;
1954 struct hud_context *hud;
1955 unsigned i;
1956 unsigned default_period_ms = 500;/* default period (1/2 second) */
1957 const char *show_fps = os_get_option("LIBGL_SHOW_FPS");
1958 bool emulate_libgl_show_fps = false;
1959 if (show_fps) {
1960 default_period_ms = atoi(show_fps) * 1000;
1961 if (default_period_ms)
1962 emulate_libgl_show_fps = true;
1963 else
1964 default_period_ms = 500;
1965 }
1966 const char *env = debug_get_option("GALLIUM_HUD",
1967 emulate_libgl_show_fps ? "stdout,fps" : NULL);
1968 #if DETECT_OS_POSIX
1969 unsigned signo = debug_get_num_option("GALLIUM_HUD_TOGGLE_SIGNAL", 0);
1970 static bool sig_handled = false;
1971 struct sigaction action;
1972
1973 memset(&action, 0, sizeof(action));
1974 #endif
1975 huds_visible = debug_get_bool_option("GALLIUM_HUD_VISIBLE", !emulate_libgl_show_fps);
1976 hud_opacity = debug_get_num_option("GALLIUM_HUD_OPACITY", HUD_DEFAULT_OPACITY) / 100.0f;
1977 hud_scale = debug_get_num_option("GALLIUM_HUD_SCALE", HUD_DEFAULT_SCALE);
1978 hud_rotate = debug_get_num_option("GALLIUM_HUD_ROTATION", HUD_DEFAULT_ROTATION) % 360;
1979 if (hud_rotate < 0) {
1980 hud_rotate += 360;
1981 }
1982 if (hud_rotate % 90 != 0) {
1983 fprintf(stderr, "gallium_hud: rotation must be a multiple of 90. Falling back to 0.\n");
1984 hud_rotate = 0;
1985 }
1986
1987 if (!env || !*env)
1988 return NULL;
1989
1990 if (strcmp(env, "help") == 0) {
1991 print_help(screen);
1992 return NULL;
1993 }
1994
1995 hud = CALLOC_STRUCT(hud_context);
1996 if (!hud)
1997 return NULL;
1998
1999 /* font (the context is only used for the texture upload) */
2000 if (!util_font_create(cso->pipe, UTIL_FONT_FIXED_8X13, &hud->font)) {
2001 FREE(hud);
2002 return NULL;
2003 }
2004
2005 hud->refcount = 1;
2006
2007 static const enum pipe_format srgb_formats[] = {
2008 PIPE_FORMAT_B8G8R8A8_SRGB,
2009 PIPE_FORMAT_B8G8R8X8_SRGB
2010 };
2011 for (i = 0; i < ARRAY_SIZE(srgb_formats); i++) {
2012 if (!screen->is_format_supported(screen, srgb_formats[i],
2013 PIPE_TEXTURE_2D, 0, 0,
2014 PIPE_BIND_RENDER_TARGET))
2015 break;
2016 }
2017
2018 hud->has_srgb = (i == ARRAY_SIZE(srgb_formats));
2019
2020 /* blend state */
2021 hud->no_blend.rt[0].colormask = PIPE_MASK_RGBA;
2022
2023 hud->alpha_blend.rt[0].colormask = PIPE_MASK_RGBA;
2024 hud->alpha_blend.rt[0].blend_enable = 1;
2025 hud->alpha_blend.rt[0].rgb_func = PIPE_BLEND_ADD;
2026 hud->alpha_blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
2027 hud->alpha_blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
2028 hud->alpha_blend.rt[0].alpha_func = PIPE_BLEND_ADD;
2029 hud->alpha_blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ZERO;
2030 hud->alpha_blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
2031
2032 /* rasterizer */
2033 hud->rasterizer.half_pixel_center = 1;
2034 hud->rasterizer.bottom_edge_rule = 1;
2035 hud->rasterizer.depth_clip_near = 1;
2036 hud->rasterizer.depth_clip_far = 1;
2037 hud->rasterizer.line_width = 1;
2038 hud->rasterizer.line_last_pixel = 1;
2039
2040 hud->rasterizer_aa_lines = hud->rasterizer;
2041 hud->rasterizer_aa_lines.line_smooth = 1;
2042
2043 /* vertex elements */
2044 hud_init_velems(&hud->velems, 2 * sizeof(float));
2045 hud_init_velems(&hud->text_velems, 4 * sizeof(float));
2046
2047 /* sampler state (for font drawing) */
2048 hud->font_sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2049 hud->font_sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2050 hud->font_sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2051
2052 /* constants */
2053 hud->constbuf.buffer_size = sizeof(hud->constants);
2054 hud->constbuf.user_buffer = &hud->constants;
2055
2056 list_inithead(&hud->pane_list);
2057
2058 /* setup sig handler once for all hud contexts */
2059 #if DETECT_OS_POSIX
2060 if (!sig_handled && signo != 0) {
2061 action.sa_sigaction = &signal_visible_handler;
2062 action.sa_flags = SA_SIGINFO;
2063
2064 if (signo >= NSIG)
2065 fprintf(stderr, "gallium_hud: invalid signal %u\n", signo);
2066 else if (sigaction(signo, &action, NULL) < 0)
2067 fprintf(stderr, "gallium_hud: unable to set handler for signal %u\n", signo);
2068 fflush(stderr);
2069
2070 sig_handled = true;
2071 }
2072 #endif
2073
2074 if (record_ctx == 0)
2075 hud_set_record_context(hud, cso->pipe);
2076 if (draw_ctx == 0)
2077 hud_set_draw_context(hud, cso, st, st_invalidate_state);
2078
2079 hud_parse_env_var(hud, screen, env, default_period_ms);
2080 return hud;
2081 }
2082
2083 /**
2084 * Destroy a HUD. If the HUD has several users, decrease the reference counter
2085 * and detach the context from the HUD.
2086 */
2087 void
hud_destroy(struct hud_context * hud,struct cso_context * cso)2088 hud_destroy(struct hud_context *hud, struct cso_context *cso)
2089 {
2090 if (!cso || hud->record_pipe == cso->pipe)
2091 hud_unset_record_context(hud);
2092
2093 if (!cso || hud->cso == cso)
2094 hud_unset_draw_context(hud);
2095
2096 if (p_atomic_dec_zero(&hud->refcount)) {
2097 pipe_resource_reference(&hud->font.texture, NULL);
2098 FREE(hud);
2099 }
2100 }
2101
2102 void
hud_add_queue_for_monitoring(struct hud_context * hud,struct util_queue_monitoring * queue_info)2103 hud_add_queue_for_monitoring(struct hud_context *hud,
2104 struct util_queue_monitoring *queue_info)
2105 {
2106 assert(!hud->monitored_queue);
2107 hud->monitored_queue = queue_info;
2108 }
2109