1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use crate::sys;
use super::Context;
use super::binding::CTX_MUTEX;
/// Runtime state for a Dear ImGui frame owned by an external engine schedule.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum FrameLifecycleState {
/// No Dear ImGui frame is currently open for this context.
Idle,
/// A frame was opened and can accept UI commands.
InFrame,
/// The last opened frame has been rendered and draw data is available until the next frame.
Rendered,
}
/// Options used by [`Context::prepare_frame`].
#[derive(Copy, Clone, Debug)]
pub struct FramePrepareOptions {
/// Main display size in pixels.
pub display_size: [f32; 2],
/// Time elapsed since the previous frame, in seconds.
pub delta_time: f32,
/// Optional framebuffer scale for HiDPI render targets.
pub framebuffer_scale: Option<[f32; 2]>,
/// Backend capability flags to OR into the context before opening the frame.
pub backend_flags: crate::BackendFlags,
}
impl FramePrepareOptions {
/// Create frame preparation options with display size and delta time.
pub fn new(display_size: [f32; 2], delta_time: f32) -> Self {
Self {
display_size,
delta_time,
framebuffer_scale: None,
backend_flags: crate::BackendFlags::empty(),
}
}
/// Set the framebuffer scale used by the frame.
#[must_use]
pub fn framebuffer_scale(mut self, scale: [f32; 2]) -> Self {
self.framebuffer_scale = Some(scale);
self
}
/// OR backend capability flags into the context before opening the frame.
#[must_use]
pub fn backend_flags(mut self, flags: crate::BackendFlags) -> Self {
self.backend_flags |= flags;
self
}
/// Convenience for modern renderers that support ImGui 1.92 texture requests.
#[must_use]
pub fn renderer_has_textures(self) -> Self {
self.backend_flags(crate::BackendFlags::RENDERER_HAS_TEXTURES)
}
}
/// A frame opened by [`Context::begin_frame`].
///
/// This token is intended for engine integrations that need to make the Dear ImGui frame boundary
/// explicit: one system opens the frame, several user systems draw through [`Self::ui`], and one
/// system consumes the token to render or snapshot the frame. The existing [`Context::frame`] and
/// [`Context::render`] calls remain available for traditional immediate-mode loops.
pub struct FrameToken<'ctx> {
ctx: &'ctx mut Context,
}
/// Result returned by [`Context::frame_with_result`].
pub struct FrameResult<'ctx, T> {
/// Value returned by the UI-building closure.
pub value: T,
/// Draw data produced by rendering the frame after the closure returned.
pub draw_data: &'ctx mut crate::render::DrawData,
}
impl Context {
/// Prepare IO values commonly needed before starting a frame.
///
/// This does not call `NewFrame()`. Engine backends can call it from their input/window update
/// stage and then open the actual Dear ImGui frame later in the schedule with
/// [`Context::begin_frame`].
pub fn prepare_frame(&mut self, options: FramePrepareOptions) {
let io = self.io_mut();
io.set_display_size(options.display_size);
io.set_delta_time(options.delta_time);
if let Some(scale) = options.framebuffer_scale {
io.set_display_framebuffer_scale(scale);
}
if !options.backend_flags.is_empty() {
io.set_backend_flags(io.backend_flags() | options.backend_flags);
}
}
/// Return the current frame lifecycle state for this context.
pub fn frame_lifecycle_state(&self) -> FrameLifecycleState {
let _guard = CTX_MUTEX.lock();
self.assert_current_context("Context::frame_lifecycle_state()");
self.frame_lifecycle_state_unlocked()
}
/// Begin a Dear ImGui frame and return an explicit frame token.
///
/// Engine integrations should prefer this when the frame is owned by a schedule rather than a
/// single function. Draw UI through [`FrameToken::ui`] and then consume the token with
/// [`FrameToken::render`] or [`FrameToken::render_snapshot`].
pub fn begin_frame(&mut self) -> FrameToken<'_> {
let _ = self.frame();
FrameToken { ctx: self }
}
/// Creates a new frame and returns a Ui object for building the interface.
///
/// Note: you must update `io.DisplaySize` (and usually `io.DeltaTime`) before calling this,
/// unless you are using a platform backend that does it for you (e.g. `dear-imgui-winit`).
pub fn frame(&mut self) -> &mut crate::ui::Ui {
let _guard = CTX_MUTEX.lock();
self.assert_current_context("Context::frame()");
self.assert_can_begin_frame_unlocked("Context::frame()");
unsafe {
// Dear ImGui initializes DisplaySize to (-1, -1). Calling NewFrame() without a
// platform backend (or without setting DisplaySize manually) will trip an internal
// assertion and abort the process. Fail fast with a Rust panic to make the setup
// requirement obvious.
let io = sys::igGetIO_Nil();
if !io.is_null() && ((*io).DisplaySize.x < 0.0 || (*io).DisplaySize.y < 0.0) {
panic!(
"Context::frame() called with invalid io.DisplaySize ({}, {}). \
Set io.DisplaySize (and typically io.DeltaTime) before starting a frame. \
If you are using a windowing/event-loop library, prefer a platform backend such as \
dear-imgui-winit::WinitPlatform::prepare_frame().",
(*io).DisplaySize.x,
(*io).DisplaySize.y
);
}
sys::igNewFrame();
}
&mut self.ui
}
/// Create a new frame with a callback
pub fn frame_with<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&crate::ui::Ui) -> R,
{
let ui = self.frame();
f(ui)
}
/// Begin a frame, run a UI-building closure, render the frame, and return both values.
///
/// This is a convenience for callers that want the old callback style but also want the draw
/// data produced by closing the frame. Use [`Context::begin_frame`] when the UI is built across
/// several engine systems.
pub fn frame_with_result<F, R>(&mut self, f: F) -> FrameResult<'_, R>
where
F: FnOnce(&crate::ui::Ui) -> R,
{
let frame = self.begin_frame();
let value = f(frame.ui());
let draw_data = frame.render();
FrameResult { value, draw_data }
}
/// Renders the frame and returns a mutable reference to the resulting draw data
///
/// This finalizes the Dear ImGui frame and prepares all draw data for rendering.
/// The returned draw data contains all the information needed to render the frame.
///
/// Renderer backends receive mutable draw data because ImGui 1.92 texture requests require
/// backends to write `TexID`/`Status` feedback into `ImTextureData`.
pub fn render(&mut self) -> &mut crate::render::DrawData {
let _guard = CTX_MUTEX.lock();
self.assert_current_context("Context::render()");
self.assert_can_render_unlocked("Context::render()");
unsafe {
sys::igRender();
let dd = sys::igGetDrawData();
if dd.is_null() {
panic!("Context::render() returned null draw data");
}
&mut *(dd as *mut crate::render::DrawData)
}
}
/// Render the current frame and build a thread-safe snapshot for all platform viewports.
///
/// With the `multi-viewport` feature enabled, Dear ImGui stores draw data on each viewport.
/// `Context::render()` and `igGetDrawData()` only expose the main viewport draw data, so
/// engine backends that render secondary OS windows should use this method after opening a
/// frame. The returned snapshot still keeps [`FrameSnapshot::draw`] as the main viewport for
/// compatibility and adds per-viewport draw data in
/// [`FrameSnapshot::viewports`](crate::render::snapshot::FrameSnapshot::viewports).
#[cfg(feature = "multi-viewport")]
pub fn render_platform_viewport_snapshot(
&mut self,
options: crate::render::snapshot::SnapshotOptions,
) -> Result<crate::render::snapshot::FrameSnapshot, crate::render::snapshot::SnapshotError>
{
let _ = self.render();
self.platform_viewport_snapshot(options)
}
/// Build a thread-safe snapshot from the current platform viewport draw data.
///
/// This does not call `Render()`. Call it only after a frame has already been rendered and
/// before the next frame starts. Engine integrations can use this when they need to run
/// platform-window maintenance after `Render()` but before cloning draw data for another
/// render schedule.
#[cfg(feature = "multi-viewport")]
pub fn platform_viewport_snapshot(
&mut self,
options: crate::render::snapshot::SnapshotOptions,
) -> Result<crate::render::snapshot::FrameSnapshot, crate::render::snapshot::SnapshotError>
{
let _guard = CTX_MUTEX.lock();
self.assert_current_context("Context::platform_viewport_snapshot()");
if self.frame_lifecycle_state_unlocked() != FrameLifecycleState::Rendered {
panic!(
"Context::platform_viewport_snapshot() called before rendering the current frame"
);
}
crate::render::snapshot::FrameSnapshot::from_platform_io(self.platform_io(), options)
}
/// Gets the draw data for the current frame
///
/// This returns the draw data without calling render. Only valid after
/// `render()` has been called and before the next `new_frame()`.
pub fn draw_data(&self) -> Option<&crate::render::DrawData> {
let _guard = CTX_MUTEX.lock();
self.assert_current_context("Context::draw_data()");
unsafe {
let draw_data = sys::igGetDrawData();
if draw_data.is_null() {
None
} else {
let data = &*(draw_data as *const crate::render::DrawData);
if data.valid() { Some(data) } else { None }
}
}
}
/// Gets mutable draw data for the current frame.
///
/// This returns the draw data without calling render. Only valid after `render()` has been
/// called and before the next `new_frame()`. Use this when a renderer needs to process texture
/// updates after draw data has already been produced.
pub fn draw_data_mut(&mut self) -> Option<&mut crate::render::DrawData> {
let _guard = CTX_MUTEX.lock();
self.assert_current_context("Context::draw_data_mut()");
unsafe {
let draw_data = sys::igGetDrawData();
if draw_data.is_null() {
None
} else {
let data = &mut *(draw_data as *mut crate::render::DrawData);
if data.valid() { Some(data) } else { None }
}
}
}
fn frame_lifecycle_state_unlocked(&self) -> FrameLifecycleState {
unsafe {
let raw = &*self.raw;
if raw.WithinFrameScope {
FrameLifecycleState::InFrame
} else if raw.FrameCountRendered == raw.FrameCount && raw.FrameCount > 0 {
FrameLifecycleState::Rendered
} else {
FrameLifecycleState::Idle
}
}
}
fn assert_can_begin_frame_unlocked(&self, caller: &str) {
if self.frame_lifecycle_state_unlocked() == FrameLifecycleState::InFrame {
panic!("{caller} called while another Dear ImGui frame is already open");
}
}
fn assert_can_render_unlocked(&self, caller: &str) {
if self.frame_lifecycle_state_unlocked() != FrameLifecycleState::InFrame {
panic!("{caller} called without an open Dear ImGui frame");
}
}
}
impl<'ctx> FrameToken<'ctx> {
/// Borrow the UI for this frame.
///
/// This can be called repeatedly by an engine-owned frame runner to let multiple systems draw
/// into the same frame, as long as those systems are scheduled sequentially.
pub fn ui(&self) -> &crate::ui::Ui {
&self.ctx.ui
}
/// Return the lifecycle state while this token owns the open frame.
pub fn lifecycle_state(&self) -> FrameLifecycleState {
self.ctx.frame_lifecycle_state()
}
/// Render this frame and return the resulting draw data.
pub fn render(self) -> &'ctx mut crate::render::DrawData {
self.ctx.render()
}
/// Render this frame and build a thread-safe snapshot from the resulting draw data.
///
/// This is the preferred handoff shape for render-world integrations such as Bevy, where raw
/// ImGui pointers must not cross the engine extraction boundary.
pub fn render_snapshot(
self,
options: crate::render::snapshot::SnapshotOptions,
) -> Result<crate::render::snapshot::FrameSnapshot, crate::render::snapshot::SnapshotError>
{
let draw_data = self.ctx.render();
crate::render::snapshot::FrameSnapshot::from_draw_data(draw_data, options)
}
/// Render this frame and build a thread-safe snapshot for all platform viewports.
#[cfg(feature = "multi-viewport")]
pub fn render_platform_viewport_snapshot(
self,
options: crate::render::snapshot::SnapshotOptions,
) -> Result<crate::render::snapshot::FrameSnapshot, crate::render::snapshot::SnapshotError>
{
self.ctx.render_platform_viewport_snapshot(options)
}
}