Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto render Stats #674

Merged
merged 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/imgui_renderer_sea.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ def loop():
with stats:
render()
imgui_renderer.render()
stats.render()
canvas.request_draw()


Expand Down
28 changes: 22 additions & 6 deletions wgpu/utils/imgui/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Stats:
The color of the background.
align : str
The alignment of the stats window, either "left" or "right". Default is "left".
auto_render : bool
If True, the render method will be called automatically. Default is True.

"""

Expand All @@ -32,6 +34,7 @@ def __init__(
foreground=(0, 1, 0, 1),
background=(0, 0.2, 0, 0.5),
align="left",
auto_render=True,
):
self._foreground = foreground
self._background = background
Expand Down Expand Up @@ -63,13 +66,14 @@ def __init__(
self._ms_samples = np.zeros(100, dtype=np.float32)

self._mode = 0
self._auto_render = auto_render

def _draw_imgui(self):
imgui.new_frame()

imgui.set_next_window_size((125, 0), imgui.Cond_.always)
imgui.set_next_window_size((130, 0), imgui.Cond_.always)
if self._align == "right":
pos = imgui.get_io().display_size.x - 125
pos = imgui.get_io().display_size.x - 130
else:
pos = 0

Expand All @@ -92,16 +96,16 @@ def _draw_imgui(self):
if self._fps is not None:
ms = self._ms_samples[-1]
text = f"{int(ms)} ms ({self._tmin}-{self._tmax})"
text += f"\n{self._fps}fps ({self._fmin}-{self._fmax})"
text += f"\n{self._fps} fps ({self._fmin}-{self._fmax})"
imgui.text(text)

elif self._mode == 1:
imgui.text(f"{self._fps}fps({self._fmin}-{self._fmax})")
imgui.plot_histogram("##", self._fps_samples, graph_size=(110, 25))
imgui.text(f"{self._fps} fps({self._fmin}-{self._fmax})")
imgui.plot_histogram("##", self._fps_samples, graph_size=(115, 25))
elif self._mode == 2:
ms = self._ms_samples[-1]
imgui.text(f"{int(ms)} ms({self._tmin}-{self._tmax})")
imgui.plot_lines("##", self._ms_samples, graph_size=(110, 25))
imgui.plot_lines("##", self._ms_samples, graph_size=(115, 25))

imgui.pop_style_color()

Expand All @@ -118,6 +122,15 @@ def _on_mouse(self, event):
self._mode = (self._mode + 1) % 3
event["stop_propagation"] = True

@property
def auto_render(self):
"""If True, the render method will be called automatically. Default is True."""
return self._auto_render

@auto_render.setter
def auto_render(self, value):
self._auto_render = bool(value)

def start(self):
if not self._init:
return
Expand Down Expand Up @@ -154,6 +167,9 @@ def stop(self):
self._ms_samples = np.roll(self._ms_samples, -1)
self._ms_samples[-1] = delta

if self._auto_render:
self.render()

def render(self):
self._renderer.render()

Expand Down