Skip to content

Commit 202768e

Browse files
authored
visualizer: Use Ash built-in bitflags string formatting (#38)
* visualizer: Use im_str macro for formatting * visualizer: Use Ash built-in bitflags string formatting Ash already defines a stringifyer for bitflags (see `const_debugs.rs`) that is autogenerated based on all available flags, not just what happened to be available at the time this visualizer was written.
1 parent a38b674 commit 202768e

File tree

1 file changed

+27
-72
lines changed

1 file changed

+27
-72
lines changed

src/visualizer/mod.rs

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::free_list_allocator;
55
use crate::VulkanAllocator;
66
use crate::*;
77

8-
use ash::vk;
98
use imgui::*;
109

1110
// Default value for block visualizer granularity.
@@ -195,50 +194,6 @@ impl SubAllocatorVisualizer for dedicated_block_allocator::DedicatedBlockAllocat
195194
}
196195
}
197196

198-
fn format_heap_flags(flags: vk::MemoryHeapFlags) -> String {
199-
let flag_names = ["DEVICE_LOCAL", "MULTI_INSTANCE"];
200-
201-
let mut result = String::new();
202-
let mut mask = 0x1;
203-
for flag in flag_names.iter() {
204-
if (flags.as_raw() & mask) != 0 {
205-
if !result.is_empty() {
206-
result += " | "
207-
}
208-
result += flag;
209-
}
210-
211-
mask <<= 1;
212-
}
213-
result
214-
}
215-
fn format_memory_properties(props: vk::MemoryPropertyFlags) -> String {
216-
let flag_names = [
217-
"DEVICE_LOCAL",
218-
"HOST_VISIBLE",
219-
"HOST_COHERENT",
220-
"HOST_CACHED",
221-
"LAZILY_ALLOCATED",
222-
"PROTECTED",
223-
"DEVICE_COHERENT",
224-
"DEVICE_UNCACHED",
225-
];
226-
227-
let mut result = String::new();
228-
let mut mask = 0x1;
229-
for flag in flag_names.iter() {
230-
if (props.as_raw() & mask) != 0 {
231-
if !result.is_empty() {
232-
result += " | "
233-
}
234-
result += flag;
235-
}
236-
237-
mask <<= 1;
238-
}
239-
result
240-
}
241-
242197
impl AllocatorVisualizer {
243198
pub fn new() -> Self {
244199
Self {
@@ -265,19 +220,15 @@ impl AllocatorVisualizer {
265220
));
266221

267222
let heap_count = alloc.memory_heaps.len();
268-
if CollapsingHeader::new(&ImString::new(format!(
269-
"Memory Heaps ({} heaps)",
270-
heap_count
271-
)))
272-
.build(ui)
223+
if CollapsingHeader::new(&im_str!("Memory Heaps ({} heaps)", heap_count)).build(ui)
273224
{
274225
for (i, heap) in alloc.memory_heaps.iter().enumerate() {
275226
ui.indent();
276-
if CollapsingHeader::new(&ImString::new(format!("Heap: {}", i))).build(ui) {
227+
if CollapsingHeader::new(&im_str!("Heap: {}", i)).build(ui) {
277228
ui.indent();
278229
ui.text(&format!(
279-
"flags: {} (0x{:x})",
280-
format_heap_flags(heap.flags),
230+
"flags: {:?} (0x{:x})",
231+
heap.flags,
281232
heap.flags.as_raw()
282233
));
283234
ui.text(&format!(
@@ -290,21 +241,21 @@ impl AllocatorVisualizer {
290241
}
291242
}
292243

293-
if CollapsingHeader::new(&ImString::new(format!(
244+
if CollapsingHeader::new(&im_str!(
294245
"Memory Types: ({} types)",
295246
alloc.memory_types.len()
296-
)))
247+
))
297248
.flags(TreeNodeFlags::DEFAULT_OPEN)
298249
.build(ui)
299250
{
300251
ui.indent();
301252
for (mem_type_i, mem_type) in alloc.memory_types.iter().enumerate() {
302-
if CollapsingHeader::new(&ImString::new(format!(
253+
if CollapsingHeader::new(&im_str!(
303254
"Type: {} ({} blocks)###Type{}",
304255
mem_type_i,
305256
mem_type.memory_blocks.len(),
306257
mem_type_i
307-
)))
258+
))
308259
.build(ui)
309260
{
310261
let mut total_block_size = 0;
@@ -314,8 +265,8 @@ impl AllocatorVisualizer {
314265
total_allocated += block.sub_allocator.allocated();
315266
}
316267
ui.text(&format!(
317-
"properties: {} (0x{:x})",
318-
format_memory_properties(mem_type.memory_properties),
268+
"properties: {:?} (0x{:x})",
269+
mem_type.memory_properties,
319270
mem_type.memory_properties.as_raw()
320271
));
321272
ui.text(&format!("heap index: {}", mem_type.heap_index));
@@ -333,11 +284,12 @@ impl AllocatorVisualizer {
333284
ui.text(&format!("block count: {}", active_block_count));
334285
for (block_i, block) in mem_type.memory_blocks.iter().enumerate() {
335286
if let Some(block) = block {
336-
TreeNode::new(&ImString::new(format!(
287+
TreeNode::new(&im_str!(
337288
"Block: {}##memtype({})",
338-
block_i, mem_type_i
339-
)))
340-
.label(&ImString::new(format!("Block: {}", block_i)))
289+
block_i,
290+
mem_type_i
291+
))
292+
.label(&im_str!("Block: {}", block_i))
341293
.build(ui, || {
342294
use ash::vk::Handle;
343295
ui.indent();
@@ -358,11 +310,12 @@ impl AllocatorVisualizer {
358310
block.sub_allocator.draw_base_info(ui);
359311

360312
if block.sub_allocator.supports_visualization() {
361-
let button_name = format!(
313+
let button_name = im_str!(
362314
"visualize##memtype({})block({})",
363-
mem_type_i, block_i
315+
mem_type_i,
316+
block_i
364317
);
365-
if ui.small_button(&ImString::new(button_name)) {
318+
if ui.small_button(&button_name) {
366319
match self
367320
.selected_blocks
368321
.iter()
@@ -412,10 +365,11 @@ impl AllocatorVisualizer {
412365
false
413366
};
414367
let mut is_open = true;
415-
imgui::Window::new(&imgui::ImString::new(format!(
368+
imgui::Window::new(&imgui::im_str!(
416369
"Block Visualizer##memtype({})block({})",
417-
window.memory_type_index, window.block_index
418-
)))
370+
window.memory_type_index,
371+
window.block_index
372+
))
419373
.size([1920.0 * 0.5, 1080.0 * 0.5], imgui::Condition::FirstUseEver)
420374
.title_bar(true)
421375
.scroll_bar(true)
@@ -454,10 +408,11 @@ impl AllocatorVisualizer {
454408
.max(BYTES_PER_UNIT_MIN);
455409

456410
// Draw the visualization in a child window.
457-
imgui::ChildWindow::new(&ImString::new(format!(
411+
imgui::ChildWindow::new(&im_str!(
458412
"Visualization Sub-window##memtype({})block({})",
459-
window.memory_type_index, window.block_index
460-
)))
413+
window.memory_type_index,
414+
window.block_index
415+
))
461416
.scrollable(true)
462417
.scroll_bar(true)
463418
.build(ui, || {

0 commit comments

Comments
 (0)