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
// Renderer font atlas handling (prepare & fallback upload)
use super::*;
use dear_imgui_rs::Context;
impl WgpuRenderer {
/// Load font texture from Dear ImGui context
///
/// With the new texture management system in Dear ImGui 1.92+, font textures are
/// automatically managed through ImDrawData->Textures[] during rendering.
/// Do not manually call `fonts.build()` here: with ImGui 1.92+ this is handled by
/// `ImFontAtlasUpdateNewFrame()` when `BackendFlags::RENDERER_HAS_TEXTURES` is set, and
/// calling Build() in the legacy mode can trigger assertions on the next frame.
pub(super) fn reload_font_texture(
&mut self,
imgui_ctx: &mut Context,
_device: &Device,
_queue: &Queue,
) -> RendererResult<()> {
let _ = imgui_ctx;
Ok(())
}
/// Legacy/fallback path: upload font atlas texture immediately and assign TexID.
/// Returns Some(tex_id) on success, None if texdata is unavailable.
pub(super) fn try_upload_font_atlas_legacy(
&mut self,
imgui_ctx: &mut Context,
device: &Device,
queue: &Queue,
) -> RendererResult<Option<u64>> {
// SAFETY: Access raw TexData/bytes only to copy pixels. Requires fonts.build() called.
let fonts = imgui_ctx.font_atlas();
// Try to read raw texture data to determine bytes-per-pixel
let raw_tex = fonts.get_tex_data();
if raw_tex.is_null() {
if cfg!(debug_assertions) {
tracing::debug!(
target: "dear-imgui-wgpu",
"[dear-imgui-wgpu][debug] Font atlas TexData is null; skip legacy upload"
);
}
return Ok(None);
}
// Read metadata
let (width, height, bpp, pixels_slice): (u32, u32, i32, Option<&[u8]>) = unsafe {
let w = (*raw_tex).Width as u32;
let h = (*raw_tex).Height as u32;
let bpp = (*raw_tex).BytesPerPixel;
let px_ptr = (*raw_tex).Pixels as *const u8;
if px_ptr.is_null() || w == 0 || h == 0 || bpp <= 0 {
(w, h, bpp, None)
} else {
let bpp_usize = match usize::try_from(bpp) {
Ok(v) if v > 0 => v,
_ => 0,
};
let size = (w as usize)
.checked_mul(h as usize)
.and_then(|v| v.checked_mul(bpp_usize));
match size {
Some(size) => (w, h, bpp, Some(std::slice::from_raw_parts(px_ptr, size))),
None => (w, h, bpp, None),
}
}
};
if let Some(src) = pixels_slice {
if cfg!(debug_assertions) {
tracing::debug!(
target: "dear-imgui-wgpu",
"[dear-imgui-wgpu][debug] Font atlas texdata: {}x{} bpp={} (fallback upload for font atlas)",
width, height, bpp
);
}
// Convert to RGBA8 if needed
let (format, converted): (wgpu::TextureFormat, Vec<u8>) = if bpp == 4 {
(wgpu::TextureFormat::Rgba8Unorm, src.to_vec())
} else if bpp == 1 {
// Alpha8 -> RGBA8 (white RGB + alpha)
let px_count = match (width as usize).checked_mul(height as usize) {
Some(v) => v,
None => return Ok(None),
};
let cap = match px_count.checked_mul(4) {
Some(v) => v,
None => return Ok(None),
};
let mut out = Vec::with_capacity(cap);
for &a in src.iter() {
out.extend_from_slice(&[255, 255, 255, a]);
}
(wgpu::TextureFormat::Rgba8Unorm, out)
} else {
// Unexpected format; don't proceed
if cfg!(debug_assertions) {
tracing::debug!(
target: "dear-imgui-wgpu",
"[dear-imgui-wgpu][debug] Unexpected font atlas bpp={} -> skip",
bpp
);
}
return Ok(None);
};
// Create WGPU texture
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("Dear ImGui Font Atlas"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
// Write with 256-byte aligned row pitch
let bpp = 4u32;
let unpadded = width * bpp;
let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT;
let padded = unpadded.div_ceil(align) * align;
if padded == unpadded {
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&converted,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(unpadded),
rows_per_image: Some(height),
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
} else {
let mut padded_buf = vec![0u8; (padded * height) as usize];
for row in 0..height as usize {
let src = row * (unpadded as usize);
let dst = row * (padded as usize);
padded_buf[dst..dst + (unpadded as usize)]
.copy_from_slice(&converted[src..src + (unpadded as usize)]);
}
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&padded_buf,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(padded),
rows_per_image: Some(height),
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
if cfg!(debug_assertions) {
tracing::debug!(
target: "dear-imgui-wgpu",
"[dear-imgui-wgpu][debug] Upload font atlas with padded row pitch: unpadded={} padded={}",
unpadded, padded
);
}
}
// Register texture and set IDs so draw commands can bind it
let tex_id = self
.texture_manager
.register_texture(crate::WgpuTexture::new(texture, view));
// Set atlas texture id + status OK (updates TexRef and TexData)
{
let mut fonts_mut = imgui_ctx.font_atlas_mut();
fonts_mut.set_texture_id(dear_imgui_rs::TextureId::from(tex_id));
}
if cfg!(debug_assertions) {
tracing::debug!(
target: "dear-imgui-wgpu",
"[dear-imgui-wgpu][debug] Font atlas fallback upload complete: tex_id={}",
tex_id
);
}
return Ok(Some(tex_id));
}
if cfg!(debug_assertions) {
tracing::debug!(
target: "dear-imgui-wgpu",
"[dear-imgui-wgpu][debug] Font atlas has no CPU pixel buffer; skipping fallback upload (renderer will use modern texture updates)"
);
}
Ok(None)
}
}