Skip to content

Commit bf1235e

Browse files
authored
Support extra data in stats (#712)
* support extra data in stats * ruff
1 parent 0763b1f commit bf1235e

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

examples/imgui_renderer_sea.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ def gui(app_state):
342342
)
343343

344344

345+
stats_data = {
346+
"time": 0.0,
347+
"frame": 0,
348+
}
349+
350+
345351
def render():
346352
global global_time
347353
current_time = time.perf_counter()
@@ -373,12 +379,17 @@ def render():
373379

374380
device.queue.submit([command_encoder.finish()])
375381

382+
stats_data["time"] = f"{uniform_data['time']:.3f}"
383+
stats_data["frame"] += 1
384+
376385

377386
# set the GUI update function that gets called to return the draw data
378387
imgui_renderer.set_gui(lambda: gui(app_state))
379388

380389
stats = Stats(device, canvas, align="right")
381390

391+
stats.extra_data = stats_data
392+
382393

383394
def loop():
384395
with stats:

wgpu/utils/imgui/stats.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class Stats:
2323
The alignment of the stats window, either "left" or "right". Default is "left".
2424
auto_render : bool
2525
If True, the render method will be called automatically. Default is True.
26-
26+
extra_data : dict
27+
A dictionary of extra data (key, value) to display in the stats window. Default is None.
2728
"""
2829

2930
def __init__(
@@ -35,6 +36,7 @@ def __init__(
3536
background=(0, 0.2, 0, 0.5),
3637
align="left",
3738
auto_render=True,
39+
extra_data=None,
3840
):
3941
self._foreground = foreground
4042
self._background = background
@@ -66,14 +68,18 @@ def __init__(
6668
self._ms_samples = np.zeros(100, dtype=np.float32)
6769

6870
self._mode = 0
69-
self._auto_render = auto_render
71+
72+
self.auto_render = auto_render
73+
self.extra_data = extra_data or {}
74+
self.__window_size = None
7075

7176
def _draw_imgui(self):
7277
imgui.new_frame()
7378

74-
imgui.set_next_window_size((130, 0), imgui.Cond_.always)
75-
if self._align == "right":
76-
pos = imgui.get_io().display_size.x - 130
79+
imgui.set_next_window_size((0, 0), imgui.Cond_.always)
80+
81+
if self._align == "right" and self.__window_size is not None:
82+
pos = imgui.get_io().display_size.x - self.__window_size.x
7783
else:
7884
pos = 0
7985

@@ -107,6 +113,12 @@ def _draw_imgui(self):
107113
imgui.text(f"{int(ms)} ms({self._tmin}-{self._tmax})")
108114
imgui.plot_lines("##", self._ms_samples, graph_size=(115, 25))
109115

116+
if self._extra_data:
117+
imgui.separator()
118+
for k, v in self._extra_data.items():
119+
imgui.text(f"{k}: {v}")
120+
121+
self.__window_size = imgui.get_window_size()
110122
imgui.pop_style_color()
111123

112124
imgui.end()
@@ -131,6 +143,16 @@ def auto_render(self):
131143
def auto_render(self, value):
132144
self._auto_render = bool(value)
133145

146+
@property
147+
def extra_data(self):
148+
"""A dictionary of extra data (key, value) to display in the stats window."""
149+
return self._extra_data
150+
151+
@extra_data.setter
152+
def extra_data(self, value):
153+
assert isinstance(value, dict), "extra_data must be a dictionary"
154+
self._extra_data = value
155+
134156
def start(self):
135157
if not self._init:
136158
return

0 commit comments

Comments
 (0)