internal void accum_dt_ms(Delta_Time_Accum &accum, f32 dt) { if (accum.count < accum.max) { assert(accum.start == 0); accum.base[accum.count++] = dt; } else { accum.base[accum.start++] = dt; if (accum.start == accum.max) accum.start = 0; } } internal f32 calc_avg_dt_ms(const Delta_Time_Accum &accum) { f32 res = 0; for (u16 idx = 0; idx < accum.count; ++idx) res += accum.base[idx]; if (accum.count) res /= accum.count; return res; } internal ImColor imcol(const f32 col[3]) { return ImColor(col[0], col[1], col[2]); } internal u32 mem_edit_bg_color_fn(const u8 *, u64 off, void *user_data) { App_State *app = reinterpret_cast(user_data); off += app->base_display_addr; i64 hilite_cluster = app->viewer.highlight_cluster ? app->viewer.highlighted_cluster : -1; Section section = find_section(*app, off, hilite_cluster); if (section.highlighted) return imcol(app->viewer.col_highlight); if (section.id == Sec_Page && off == section.range.start) return imcol(app->viewer.col_page_start); if (off < section.range.start) return imcol(app->viewer.col_key); if (section.range.end() - section.post_size <= off && off < section.range.end()) return imcol(app->viewer.col_checksum); return imcol(app->viewer.col_section[section.id]); } internal void mem_edit_interact_fn(const u8 *, u64 off, void *user_data) { } internal MemoryEditor make_memory_editor(App_State &app, i32 n_cols) { MemoryEditor mem_edit; mem_edit.Cols = n_cols ? n_cols : 32; mem_edit.OptShowDataPreview = true; // Do nothing on write. // Note that we don't use ReadOnly = true because that disables selecting bytes mem_edit.WriteFn = [] (ImU8*, size_t, ImU8) {}; mem_edit.BgColorFn = mem_edit_bg_color_fn; mem_edit.BgColorFnUserData = &app; mem_edit.InteractFn = mem_edit_interact_fn; mem_edit.InteractFnUserData = &app; return mem_edit; } internal void make_viewer(App_State &app, u16 n_cols) { Viewer viewer {}; viewer.mem_edit = make_memory_editor(app, (i32)n_cols); #define COL(c, r, g, b) viewer.c[0] = r/255.0, viewer.c[1] = g/255.0, viewer.c[2] = b/255.0 COL(col_key, 0, 100, 50); COL(col_page_start, 200, 0, 200); COL(col_checksum, 134, 65, 25); COL(col_highlight, 190, 190, 190); #define COL_S(c, r, g, b) viewer.col_section[c][0] = r/255.0, viewer.col_section[c][1] = g/255.0, viewer.col_section[c][2] = b/255.0 COL_S(Sec_RNTuple_Anchor, 150, 150, 0); COL_S(Sec_RNTuple_Header, 150, 0, 50); COL_S(Sec_RNTuple_Footer, 50, 0, 150); COL_S(Sec_TFile_Header, 90, 90, 90); COL_S(Sec_TFile_Object, 120, 120, 120); COL_S(Sec_TFile_Info, 95, 76, 76); COL_S(Sec_TFile_FreeList, 60, 60, 90); COL_S(Sec_Page, 125, 0, 125); COL_S(Sec_Page_List, 60, 110, 120); COL_S(Sec_TKey_List, 100, 140, 100); #undef COL #undef COL_S app.viewer = viewer; } internal void viewer_jump_to(App_State &app, u64 addr) { app.base_display_addr = addr; app.viewer.mem_edit.GotoAddr = 0; } internal void viewer_jump_to_page(App_State &app, u64 page_idx) { assert(app.rndata.n_pages > 0); page_idx = (page_idx + app.rndata.n_pages) % app.rndata.n_pages; // @Speed Page_Info_Node *page = app.rndata.pages; for (u64 i = 0; i < page_idx; ++i) { page = page->next; assert(page); } app.viewer.latest_page_gone_to = page_idx; viewer_jump_to(app, page->range.start); } internal void viewer_jump_to_page_list(App_State &app, u64 page_list_idx) { assert(app.rndata.n_cluster_groups > 0); page_list_idx = (page_list_idx + app.rndata.n_cluster_groups) % app.rndata.n_cluster_groups; Cluster_Group_Info &cg_info = app.rndata.cluster_groups[page_list_idx]; app.viewer.latest_page_list_gone_to = page_list_idx; viewer_jump_to(app, cg_info.rng_page_list.start); } internal void viewer_jump_to_cluster(App_State &app, u64 cluster_idx) { assert(app.rndata.n_clusters > 0); cluster_idx = (cluster_idx + app.rndata.n_clusters) % app.rndata.n_clusters; // @Speed: this is slow! Consider an acceleration structure, or maybe we can reuse // Page_Info_Groups + binary search? (depends on whether cluster_idx are sorted) Page_Info_Node *page = app.rndata.pages; for (u64 i = 0; i < app.rndata.n_pages; ++i) { if (page->cluster_id == cluster_idx) break; page = page->next; assert(page); } app.viewer.highlighted_cluster = cluster_idx; viewer_jump_to(app, page->range.start); } internal void update_and_render(Arena *arena, App_State &app, f32 delta_time_ms) { accum_dt_ms(app.delta_time_accum, delta_time_ms); ImGui::SetNextWindowPos({ 0, 0 }); ImGui::SetNextWindowSize({ (f32)app.win_data.width, (f32)app.win_data.height }); Temp scratch = scratch_begin(&arena, 1); defer { scratch_end(scratch); }; const auto main_win_flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDecoration; if (ImGui::Begin("main", nullptr, main_win_flags)) { String8 ntpl_desc = rntuple_description(scratch.arena, app.rndata); ImGui::Text("RNTuple '%s' (%s) from file \"%s\"", app.ntpl_name.c(), ntpl_desc.c(), app.inspected_file.name.c()); // Draw stats { ImGui::SameLine(); f32 avg_dt = calc_avg_dt_ms(app.delta_time_accum); String8 mem_used = to_pretty_size(scratch.arena, arena->mem_used); String8 mem_peak = to_pretty_size(scratch.arena, arena->mem_peak_used); String8 stat_txt = push_str8f(scratch.arena, "mem used: %s (peak: %s) | avg dt: %.1f", mem_used.c(), mem_peak.c(), avg_dt); f32 pos_x = (ImGui::GetCursorPosX() + ImGui::GetColumnWidth() - ImGui::CalcTextSize(stat_txt.c()).x - ImGui::GetScrollX() - 2 * ImGui::GetStyle().ItemSpacing.x); if (pos_x > ImGui::GetCursorPosX()) ImGui::SetCursorPosX(pos_x); ImGui::Text("%s", stat_txt.c()); } ImGui::Separator(); // Draw main content { const i64 step_i64 = 1; ImGui::BeginTable("Hex View", 2, ImGuiTableFlags_Resizable); ImGui::TableNextColumn(); assert(app.base_display_addr < app.inspected_file.size); void *content = app.inspected_file.mem + app.base_display_addr; u64 content_size = app.inspected_file.size - app.base_display_addr; app.last_pinfo = &invalid_pinfo; app.viewer.mem_edit.DrawContents(content, content_size, app.base_display_addr); ImGui::TableNextColumn(); ImGuiColorEditFlags flags = ImGuiColorEditFlags_NoInputs|ImGuiColorEditFlags_NoLabel; // Unique sections: just display a button that jumps to the start of it and show their size for (u32 i = 0; i < Sec_COUNT; ++i) { Byte_Range range = get_section_range(app, static_cast(i)); if (!range.len) continue; String8 sec_name = section_names[i]; String8 col_label = push_str8f(scratch.arena, "_%s", sec_name.c()); ImGui::ColorEdit3(col_label.c(), app.viewer.col_section[i], flags); ImGui::SameLine(); if (ImGui::Button(sec_name.c())) viewer_jump_to(app, range.start); ImGui::SameLine(); ImGui::Text("%s", to_pretty_size(scratch.arena, range.len).c()); } // Repeated sections: allow jumping to the N-th ImGui::ColorEdit3("_Page Start", app.viewer.col_page_start, flags); ImGui::SameLine(); if (ImGui::Button("Page Start")) viewer_jump_to_page(app, 0); ImGui::ColorEdit3("_Page", app.viewer.col_section[Sec_Page], flags); ImGui::SameLine(); if (ImGui::Button("Page")) viewer_jump_to_page(app, app.viewer.latest_page_gone_to); ImGui::SameLine(); { const i64 step_fast_i64 = app.rndata.n_pages / 100; i64 page_to_go_to = app.viewer.latest_page_gone_to; ImGui::PushItemWidth(100.f); if (ImGui::InputScalar("##page_viewed", ImGuiDataType_S64, &page_to_go_to, &step_i64, &step_fast_i64, "%u")) viewer_jump_to_page(app, page_to_go_to); ImGui::PopItemWidth(); } ImGui::SameLine(); ImGui::Text("%s", to_pretty_size(scratch.arena, app.rndata.tot_page_size).c()); ImGui::ColorEdit3("_Checksum", app.viewer.col_checksum, flags); ImGui::SameLine(); if (ImGui::Button("Checksum")) {} // TODO jump to next checksum ImGui::ColorEdit3("_Page List", app.viewer.col_section[Sec_Page_List], flags); ImGui::SameLine(); if (ImGui::Button("Page List")) viewer_jump_to_page_list(app, app.viewer.latest_page_list_gone_to); ImGui::SameLine(); { i64 page_list_to_go_to = app.viewer.latest_page_list_gone_to; ImGui::PushItemWidth(80.f); if (ImGui::InputScalar("##page_list_viewed", ImGuiDataType_S64, &page_list_to_go_to, &step_i64, nullptr, "%u")) viewer_jump_to_page_list(app, page_list_to_go_to); ImGui::PopItemWidth(); } ImGui::SameLine(); ImGui::Text("%s", to_pretty_size(scratch.arena, app.rndata.tot_page_list_size).c()); // ------------------------------- ImGui::Separator(); String8 root_version_str = push_str8f(scratch.arena, "%u.%u.%u", app.tfile_data.root_version_major, app.tfile_data.root_version_minor, app.tfile_data.root_version_patch); ImGui::Text("ROOT version: %s", root_version_str.c()); ImGui::Text("TFile compression: %u", app.tfile_data.compression); ImGui::Text("Num pages: %lu", app.rndata.n_pages); ImGui::Text("Num elements: %lu", app.rndata.n_elems); { const i64 step_fast_i64 = app.rndata.n_clusters / 100; i64 cluster_to_highlight = app.viewer.highlighted_cluster; ImGui::PushItemWidth(100.f); if (ImGui::InputScalar("##highlighted_cluster", ImGuiDataType_S64, &cluster_to_highlight, &step_i64, &step_fast_i64, "%u")) { app.viewer.highlight_cluster = true; viewer_jump_to_cluster(app, cluster_to_highlight); } ImGui::PopItemWidth(); ImGui::SameLine(); ImGui::Checkbox("Highlight cluster", &app.viewer.highlight_cluster); } ImGui::EndTable(); } ImGui::End(); } }