Files
Triforce-Source/gpu/vk/vk_imgui.cpp
ruckus7382sfgsgdf 34b3683441 Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:32:48 -06:00

539 lines
27 KiB
C++

// (hookshot) Dear ImGui (third_party/imgui) over the window's image (vk_imgui.h): at the window's size, after the game's picture,
// its scaling and frame generation, in present_image. The overlays are drawn with ImGui's draw lists (vk_menu.cpp, vk_ui.h);
// ImGui takes no input of its own: the menu keeps its keys, gamepad and mouse (vk_menu.cpp menu_tick).
// Textures (the fonts' glyphs, the logo) are copied in on the present's own command buffer, before its render pass: the
// backend's own upload (a submit and vkQueueWaitIdle on the queue) is never used, so a new glyph never stalls the game.
// Fonts: the game's own menu font, read in memory from the player's game files (romfs Font2, vk_game_font.h), never written
// anywhere; else Libertinus (SIL OFL 1.1, third_party/fonts, compiled in by nx2/ui_fonts.py).
#include "vk_imgui.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <atomic>
#include <cstring>
#include <string>
#include <thread>
#include <vector>
#include "imgui.h"
#include "imgui_internal.h" // (RegisterUserTexture: the logo)
#include "backends/imgui_impl_vulkan.h"
#include "vk_game_font.h" // (third_party/zstd: the game's font files)
#include "vk_game_tex.h" // (Triforce: the game's menu textures)
#include "vk_paths.h" // (the game folder: the button icons the host served)
#include "vk_menu.h" // (opts.hdr_white: the overlays' white on an HDR10 display)
#if __has_include("hookshot_logo.h")
#include "hookshot_logo.h" // (generated by nx2/build.py: nx2/logo_rgba.py)
#endif
// (nx2/ui_fonts.py: the bundled fonts)
extern const unsigned hookshot_font_body_size, hookshot_font_strong_size, hookshot_font_heading_size;
extern const unsigned char hookshot_font_body[], hookshot_font_strong[], hookshot_font_heading[];
namespace mkwvk::ui {
namespace {
bool tried = false, ready = false;
VkFormat pipeline_format = VK_FORMAT_UNDEFINED; // (the format the backend's pipeline was made for: the window images' views')
bool srgb_target = false; // (those views are sRGB: the window's surface lists only sRGB formats and has no UNORM view of them)
Fonts fonts_;
ImTextureData* logo_ = nullptr;
float logo_aspect = 1;
int64_t presents = 0; // (frames recorded: when a texture or a staging buffer let go can be destroyed)
PFN_vkVoidFunction load(const char* name, void*) {
if (PFN_vkVoidFunction f = vkGetDeviceProcAddr(dev.device, name)) return f;
return vkx::instance_proc_addr()(dev.instance, name);
}
// ---- the game's own menu font, from the player's game files, in memory (vk_game_font.h) ----
/// The game's romfs folder: the one the game runs from (--romfs, or romfs beside the executable: host/hos_main.c)
std::string romfs_dir() {
if (host && host->romfs_dir)
if (const char* d = host->romfs_dir(); d && *d) return d;
return "";
}
game_font::Reader& font_reader(bool* ok) {
static game_font::Reader reader;
static bool opened = false, open = false;
if (!opened) opened = true, open = reader.open(romfs_dir());
*ok = open;
return reader;
}
ImFont* add_font(std::vector<unsigned char>& bytes, const char* name) {
void* font = IM_ALLOC(bytes.size()); // (the atlas owns it: in memory only)
memcpy(font, bytes.data(), bytes.size());
ImFontConfig cfg;
snprintf(cfg.Name, sizeof cfg.Name, "%s", name);
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(font, (int)bytes.size(), 18.0f, &cfg);
}
ImFont* game_font(const char* name, bool latin = true) {
bool open = false;
game_font::Reader& reader = font_reader(&open);
if (!open) return nullptr;
std::vector<unsigned char> bytes = reader.read(name, latin);
if (bytes.empty()) return nullptr;
return add_font(bytes, name);
}
ImFont* bundled_font(const unsigned char* data, unsigned size, const char* name) {
ImFontConfig cfg;
cfg.FontDataOwnedByAtlas = false; // (the compiled-in bytes)
snprintf(cfg.Name, sizeof cfg.Name, "%s", name);
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF((void*)data, (int)size, 18.0f, &cfg);
}
/// The fonts: the game's own when its files have them (all three or none, so the menu never mixes the two), else Libertinus
void load_fonts() {
const char* off = getenv("RX_VK_UI_GAME_FONT"); // (0: Libertinus whatever the game's files have)
ImFont* body = nullptr;
ImFont* strong = nullptr;
ImFont* heading = nullptr;
if (!(off && *off == '0')) {
body = game_font("ClarimoUDPE-Regular");
strong = body ? game_font("ClarimoUDPE-DemiBold") : nullptr;
heading = strong ? game_font("CetraDisplayPro-Medium") : nullptr;
}
if (body && strong && heading) {
logf("vk: ui: the game's own menu font, from its files");
} else {
ImGui::GetIO().Fonts->Clear();
body = bundled_font(hookshot_font_body, hookshot_font_body_size, "Libertinus Sans");
strong = bundled_font(hookshot_font_strong, hookshot_font_strong_size, "Libertinus Sans Bold");
heading = bundled_font(hookshot_font_heading, hookshot_font_heading_size, "Libertinus Serif Semibold");
logf("vk: ui: the bundled font (Libertinus): the game's files have no menu font");
}
fonts_ = {body, strong, heading};
// (Triforce: the game's menus are set in Cetra Display, Font2's Font.bfarc: "Normal" its Regular, "Special" its Medium;
// the HUD's counters in NinDev-Number)
if (!(off && *off == '0')) fonts_.menu = game_font("CetraDisplayPro-Regular"), fonts_.number = game_font("NinDev-Number", false);
}
// ---- (Triforce) the game's button icons: its "External" font, as the game was served it this run ----
bool glyphs_wanted = false, glyphs_tried = false;
void load_glyphs() {
glyphs_tried = true;
std::vector<unsigned char> font;
const char* from = "the game's own";
if (host && host->prompt_face && host->prompt_face() >= 0) { // (host/prompts.c: prompts/External.bfotf.zs, no dictionary)
char exe[MAX_PATH];
const DWORD n = game_module_path(exe, MAX_PATH);
std::string dir(exe, n);
dir = dir.substr(0, dir.find_last_of("\\/") + 1);
std::vector<unsigned char> packed, raw;
if (game_font::read_file(dir + "prompts/External.bfotf.zs", packed) && game_font::unzstd(packed, nullptr, raw))
font = game_font::bfotf_font(raw), from = "this controller's (prompts/External.bfotf.zs)";
}
if (font.empty()) {
bool open = false;
game_font::Reader& reader = font_reader(&open);
if (open) font = reader.read("External", false);
}
if (font.empty()) {
logf("vk: ui: no button icons (the game's files have no External font): the prompts name the buttons");
return;
}
fonts_.glyph = add_font(font, "External");
logf("vk: ui: the button icons: %s", from);
}
// ---- (Triforce) the game's menu textures (vk_game_tex.h): read and decoded on a thread of its own, made ImGui textures at
// the next begin; copied to the GPU as the logo is, on a present's command buffer ----
std::atomic<int> art_state{0}; // (0 not asked, 1 being read, 2 read, 3 made textures, 4 none)
game_tex::Skin skin_;
struct Named {
std::string name;
Art art;
};
std::vector<Named> arts_;
void art_thread() {
game_font::Reader reader; // (its own: the fonts' is the presenting thread's)
std::vector<unsigned char> bytes;
const int64_t t0 = prof_now();
const bool ok = reader.open(romfs_dir()) && reader.read_packed("UI/Layout2Archive/Common.Product.100.Nin_Ounce_NVN2.bntx.zs", bytes) &&
game_tex::make_skin(std::move(bytes), skin_);
if (ok) logf("vk: ui: the game's own menu textures, from its files (%d, %.0f ms)", (int)skin_.images.size(), (prof_now() - t0) / 1e6);
else logf("vk: ui: the game's files have no menu textures: the menu draws plain plates");
art_state.store(ok ? 2 : 4, std::memory_order_release);
}
void art_start() {
int none = 0;
const char* off = getenv("RX_VK_UI_GAME_ART"); // (0: plain plates whatever the game's files have)
if (off && *off == '0') {
art_state.store(4);
return;
}
if (art_state.compare_exchange_strong(none, 1)) std::thread(art_thread).detach();
}
void art_make() { // (the presenting thread, before a frame: the images as ImGui textures)
for (game_tex::Skin::Entry& e : skin_.images) {
ImTextureData* t = IM_NEW(ImTextureData)();
t->Create(ImTextureFormat_RGBA32, (int)e.img.w, (int)e.img.h);
memcpy(t->GetPixels(), e.img.rgba.data(), e.img.rgba.size());
t->UseColors = true;
ImGui::RegisterUserTexture(t);
arts_.push_back({e.name, Art{t, (float)e.img.w, (float)e.img.h}});
}
skin_.images.clear(), skin_.images.shrink_to_fit();
art_state.store(3, std::memory_order_release);
}
// ---- textures: made and updated on the present's command buffer, let go some frames later ----
struct Tex {
VkImage image = VK_NULL_HANDLE;
VmaAllocation alloc = nullptr;
VkImageView view = VK_NULL_HANDLE;
VkDescriptorSet set = VK_NULL_HANDLE;
};
struct Dead { // (no longer used by anything recorded after `at`: gone once the frames in flight then are done)
int64_t at;
Tex tex;
VkBuffer buf = VK_NULL_HANDLE;
VmaAllocation buf_alloc = nullptr;
};
std::vector<Dead> dead;
constexpr int64_t kFramesLater = 4; // (present_image keeps three frames in flight)
void reap(bool all) {
for (size_t i = 0; i < dead.size();) {
Dead& d = dead[i];
if (!all && presents - d.at < kFramesLater) { i++; continue; }
if (d.tex.set) ImGui_ImplVulkan_RemoveTexture(d.tex.set);
if (d.tex.view) vkDestroyImageView(dev.device, d.tex.view, nullptr);
if (d.tex.image) destroy_image(d.tex.image, d.tex.alloc);
if (d.buf) destroy_buffer(d.buf, d.buf_alloc);
dead[i] = dead.back();
dead.pop_back();
}
}
/// The format the overlays draw into: the window images' views, or headless the shots' RGBA (vk_replay.cpp RX_VK_SHOT_MENU)
VkFormat target_format() { return dev.offscreen ? VK_FORMAT_R8G8B8A8_UNORM : dev.swap_view_format; }
bool is_srgb(VkFormat f) {
return f == VK_FORMAT_B8G8R8A8_SRGB || f == VK_FORMAT_R8G8B8A8_SRGB || f == VK_FORMAT_A8B8G8R8_SRGB_PACK32;
}
/// The draw lists' colours (sRGB, as vk_ui.h gives them) in linear light, for an sRGB window image that encodes them again
void linearize(ImDrawData* dd) {
static unsigned char lut[256];
if (!lut[255])
for (int i = 0; i < 256; i++) {
const float c = i / 255.0f, l = c <= 0.04045f ? c / 12.92f : std::pow((c + 0.055f) / 1.055f, 2.4f);
lut[i] = (unsigned char)(l * 255.0f + 0.5f);
}
for (ImDrawList* list : dd->CmdLists)
for (ImDrawVert& v : list->VtxBuffer)
v.col = (v.col & IM_COL32_A_MASK) | (ImU32)lut[v.col >> IM_COL32_R_SHIFT & 0xFF] << IM_COL32_R_SHIFT |
(ImU32)lut[v.col >> IM_COL32_G_SHIFT & 0xFF] << IM_COL32_G_SHIFT | (ImU32)lut[v.col >> IM_COL32_B_SHIFT & 0xFF] << IM_COL32_B_SHIFT;
}
/// (an HDR10 window image: ST 2084 (PQ) code values in Rec. 2020, framegen_hdr's) an sRGB colour as HDR10 shows it at the
/// paper white of the Insert menu's HDR rows: decoded, its primaries to Rec. 2020, its white at paper white nits, PQ coded
ImU32 hdr10_of(ImU32 c, float white) {
static float lin[256];
static unsigned char pq[4097];
static float pq_white = -1;
if (pq_white != white) {
pq_white = white;
for (int i = 0; i < 256; i++) {
const float v = i / 255.0f;
lin[i] = v <= 0.04045f ? v / 12.92f : std::pow((v + 0.055f) / 1.055f, 2.4f);
}
for (int i = 0; i <= 4096; i++) { // (linear light, 1 the paper white, to the 8-bit PQ code)
const float y = std::pow(i / 4096.0f * white / 10000.0f, 0.1593017578125f);
const float e = std::pow((0.8359375f + 18.8515625f * y) / (1 + 18.6875f * y), 78.84375f);
pq[i] = (unsigned char)std::clamp((int)(e * 255.0f + 0.5f), 0, 255);
}
}
const float r = lin[c >> IM_COL32_R_SHIFT & 0xFF], g = lin[c >> IM_COL32_G_SHIFT & 0xFF], b = lin[c >> IM_COL32_B_SHIFT & 0xFF];
auto code = [&](float v) { return (ImU32)pq[std::clamp((int)(v * 4096.0f + 0.5f), 0, 4096)]; };
return (c & IM_COL32_A_MASK) | code(0.6274f * r + 0.3293f * g + 0.0433f * b) << IM_COL32_R_SHIFT |
code(0.0691f * r + 0.9195f * g + 0.0114f * b) << IM_COL32_G_SHIFT | code(0.0164f * r + 0.0880f * g + 0.8956f * b) << IM_COL32_B_SHIFT;
}
void hdr10(ImDrawData* dd) {
const float white = (float)std::clamp(opts.hdr_white, 80, 400);
for (ImDrawList* list : dd->CmdLists)
for (ImDrawVert& v : list->VtxBuffer) v.col = hdr10_of(v.col, white);
}
void layout(VkCommandBuffer cmd, VkImage image, VkImageLayout from, VkImageLayout to, VkPipelineStageFlags2 src_stage,
VkAccessFlags2 src, VkPipelineStageFlags2 dst_stage, VkAccessFlags2 dst) {
VkImageMemoryBarrier2 b{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2};
b.srcStageMask = src_stage, b.srcAccessMask = src;
b.dstStageMask = dst_stage, b.dstAccessMask = dst;
b.oldLayout = from, b.newLayout = to;
b.srcQueueFamilyIndex = b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
b.image = image;
b.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
VkDependencyInfo d{VK_STRUCTURE_TYPE_DEPENDENCY_INFO};
d.imageMemoryBarrierCount = 1, d.pImageMemoryBarriers = &b;
vkCmdPipelineBarrier2(cmd, &d);
}
/// tex's pixels in rect (x, y, w, h), as RGBA, into dst
void pixels(ImTextureData* tex, int x, int y, int w, int h, unsigned char* dst) {
for (int row = 0; row < h; row++) {
const unsigned char* src = (const unsigned char*)tex->GetPixelsAt(x, y + row);
if (tex->Format == ImTextureFormat_RGBA32 && dev.hdr && tex != ImGui::GetIO().Fonts->TexData) { // (the logo, as HDR10 shows it)
const float white = (float)std::clamp(opts.hdr_white, 80, 400);
for (int i = 0; i < w; i++) {
ImU32 c;
memcpy(&c, src + i * 4, 4);
c = hdr10_of(c, white);
memcpy(dst + ((size_t)row * w + i) * 4, &c, 4);
}
} else if (tex->Format == ImTextureFormat_RGBA32) { // (the glyphs: white, their coverage in alpha, the same in any space)
memcpy(dst + (size_t)row * w * 4, src, (size_t)w * 4);
} else { // (Alpha8: white, its coverage as alpha)
for (int i = 0; i < w; i++) {
unsigned char* p = dst + ((size_t)row * w + i) * 4;
p[0] = p[1] = p[2] = 255, p[3] = src[i];
}
}
}
}
void update_texture(VkCommandBuffer cmd, ImTextureData* tex) {
if (tex->Status == ImTextureStatus_WantDestroy) {
if (Tex* t = (Tex*)tex->BackendUserData) dead.push_back({presents, *t}), IM_DELETE(t);
tex->SetTexID(ImTextureID_Invalid);
tex->BackendUserData = nullptr;
tex->SetStatus(ImTextureStatus_Destroyed);
return;
}
const bool create = tex->Status == ImTextureStatus_WantCreate;
if (!create && !tex->BackendUserData) return;
// (what to copy: all of it when new, else the updates queued)
ImVector<ImTextureRect> rects;
if (create) rects.push_back({0, 0, (unsigned short)tex->Width, (unsigned short)tex->Height});
else rects = tex->Updates;
if (rects.empty()) rects.push_back(tex->UpdateRect);
uint64_t size = 0;
for (const ImTextureRect& r : rects) size += (uint64_t)r.w * r.h * 4;
// (the staging buffer first, then the image: a failure leaves nothing behind, and the texture is asked for again)
VkBuffer buf = VK_NULL_HANDLE;
VmaAllocation buf_alloc = nullptr;
void* map = nullptr;
if (!size || !create_buffer(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, buf, buf_alloc, &map)) return;
if (create) {
Tex made;
VkImageCreateInfo ci{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
ci.imageType = VK_IMAGE_TYPE_2D;
// (an sRGB window image without a UNORM view blends in linear light: the logo sampled as sRGB to match, vk_ui.h's
// colours made linear in record. The glyphs are white, their coverage in alpha: the same either way)
ci.format = srgb_target ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM;
ci.extent = {(uint32_t)tex->Width, (uint32_t)tex->Height, 1};
ci.mipLevels = ci.arrayLayers = 1;
ci.samples = VK_SAMPLE_COUNT_1_BIT;
ci.tiling = VK_IMAGE_TILING_OPTIMAL;
ci.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
ci.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
bool ok = create_image(ci, made.image, made.alloc);
if (ok) {
VkImageViewCreateInfo vi{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
vi.image = made.image;
vi.viewType = VK_IMAGE_VIEW_TYPE_2D;
vi.format = ci.format;
vi.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
ok = vkCreateImageView(dev.device, &vi, nullptr, &made.view) == VK_SUCCESS;
}
if (ok) ok = (made.set = ImGui_ImplVulkan_AddTexture(made.view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)) != VK_NULL_HANDLE;
if (!ok) { // (nothing recorded uses them yet)
if (made.view) vkDestroyImageView(dev.device, made.view, nullptr);
if (made.image) destroy_image(made.image, made.alloc);
destroy_buffer(buf, buf_alloc);
return;
}
Tex* t = IM_NEW(Tex)();
*t = made;
tex->BackendUserData = t;
tex->SetTexID((ImTextureID)t->set);
}
Tex* t = (Tex*)tex->BackendUserData;
std::vector<VkBufferImageCopy> copies;
uint64_t at = 0;
for (const ImTextureRect& r : rects) {
pixels(tex, r.x, r.y, r.w, r.h, (unsigned char*)map + at);
VkBufferImageCopy c{};
c.bufferOffset = at;
c.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
c.imageOffset = {r.x, r.y, 0};
c.imageExtent = {r.w, r.h, 1};
copies.push_back(c);
at += (uint64_t)r.w * r.h * 4;
}
// Sequential-write host memory need not be coherent; publish every packed rectangle before GPU use.
flush_mapped(buf_alloc, at);
layout(cmd, t->image, create ? VK_IMAGE_LAYOUT_UNDEFINED : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT, 0, VK_PIPELINE_STAGE_2_COPY_BIT,
VK_ACCESS_2_TRANSFER_WRITE_BIT);
vkCmdCopyBufferToImage(cmd, buf, t->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, (uint32_t)copies.size(), copies.data());
layout(cmd, t->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_PIPELINE_STAGE_2_COPY_BIT,
VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT, VK_ACCESS_2_SHADER_SAMPLED_READ_BIT);
dead.push_back({presents, Tex{}, buf, buf_alloc}); // (the staging buffer: let go once this frame is done)
tex->SetStatus(ImTextureStatus_OK);
}
bool init() {
tried = true;
if (!dev.device || (!dev.offscreen && (dev.swap_views.empty() || !dev.swap_views[0]))) {
logf("vk: ui: no views of the window's images (the surface does not allow drawing into them): no menu or counter");
return false;
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
io.LogFilename = nullptr;
io.Fonts->TexDesiredFormat = ImTextureFormat_RGBA32;
if (!ImGui_ImplVulkan_LoadFunctions(VK_API_VERSION_1_3, load)) {
logf("vk: ui: the Vulkan functions did not load");
return false;
}
pipeline_format = target_format();
srgb_target = is_srgb(pipeline_format);
ImGui_ImplVulkan_InitInfo ii{};
ii.ApiVersion = VK_API_VERSION_1_3;
ii.Instance = dev.instance;
ii.PhysicalDevice = dev.physical;
ii.Device = dev.device;
ii.QueueFamily = dev.queue_family;
ii.Queue = dev.queue; // (for the backend's own uploads, never used here: update_texture)
ii.DescriptorPoolSize = 64; // (the backend's own pool: the textures' descriptor sets: the glyphs, the logo, the game's menu skin)
ii.MinImageCount = 2;
ii.ImageCount = 4; // (its vertex buffers in turn: one more than the three frames present_image keeps in flight)
ii.UseDynamicRendering = true;
ii.PipelineInfoMain.PipelineRenderingCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO;
ii.PipelineInfoMain.PipelineRenderingCreateInfo.colorAttachmentCount = 1;
ii.PipelineInfoMain.PipelineRenderingCreateInfo.pColorAttachmentFormats = &pipeline_format;
if (!ImGui_ImplVulkan_Init(&ii)) {
logf("vk: ui: the Vulkan backend did not start");
return false;
}
load_fonts();
art_start();
#ifdef HOOKSHOT_LOGO
logo_ = IM_NEW(ImTextureData)();
logo_->Create(ImTextureFormat_RGBA32, (int)hookshot_logo_w, (int)hookshot_logo_h);
memcpy(logo_->GetPixels(), hookshot_logo_rgba, (size_t)hookshot_logo_w * hookshot_logo_h * 4);
logo_->UseColors = true;
ImGui::RegisterUserTexture(logo_);
logo_aspect = (float)hookshot_logo_w / hookshot_logo_h;
#endif
logf("vk: ui: Dear ImGui %s on the window's images (format %d%s)", IMGUI_VERSION, (int)pipeline_format,
srgb_target ? ", sRGB: colours made linear" : "");
return true;
}
std::vector<std::pair<ImFont*, float>> to_bake, kept;
std::vector<float> baked_sizes;
void bake_some() {
for (int k = 0; k < 4 && !to_bake.empty(); k++) { // (a few a frame: each is up to ~100 glyphs rasterized)
ImFontBaked* b = to_bake.back().first->GetFontBaked(to_bake.back().second);
for (ImWchar c = 32; c < 127; c++) b->FindGlyph(c);
to_bake.pop_back();
}
// (the sizes baked ahead marked used each frame: ImGui lets go of sizes unused for two frames when its atlas needs room,
// and would bake them all again on the menu's first open)
for (const auto& [font, px] : kept) font->GetFontBaked(px);
}
} // namespace
bool begin(uint32_t w, uint32_t h) {
if (!tried) ready = init();
if (!ready || (!dev.offscreen && dev.swap_views.empty())) return false;
if (target_format() != pipeline_format) { // (a new swapchain of another format: create_swapchain waited for the device)
pipeline_format = target_format();
srgb_target = is_srgb(pipeline_format);
ImGui_ImplVulkan_PipelineInfo pi{};
pi.PipelineRenderingCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO;
pi.PipelineRenderingCreateInfo.colorAttachmentCount = 1;
pi.PipelineRenderingCreateInfo.pColorAttachmentFormats = &pipeline_format;
ImGui_ImplVulkan_CreateMainPipeline(&pi);
logf("vk: ui: the window's images are now format %d%s", (int)pipeline_format, srgb_target ? " (sRGB)" : "");
}
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)w, (float)h);
static int64_t last = 0;
const int64_t now = prof_now();
io.DeltaTime = last && now > last ? std::min((float)((now - last) / 1e9), 0.25f) : 1.0f / 60.0f;
last = now;
if (art_state.load(std::memory_order_acquire) == 2) art_make();
if (glyphs_wanted && !glyphs_tried) load_glyphs();
ImGui_ImplVulkan_NewFrame();
ImGui::NewFrame();
bake_some();
return true;
}
const Fonts& fonts() { return fonts_; }
const Art* art(const char* name) {
if (art_state.load(std::memory_order_acquire) != 3) return nullptr;
for (const Named& n : arts_)
if (n.name == name) return &n.art;
return nullptr;
}
void want_glyphs() { glyphs_wanted = true; }
void prebake(const float* px, int n) {
if (!ready) return;
to_bake.clear(), kept.clear(), baked_sizes.clear(); // (the sizes of the unit now only)
for (int i = 0; i < n; i++) {
const float size = std::round(px[i] * 100.0f) / 100.0f;
if (std::find(baked_sizes.begin(), baked_sizes.end(), size) != baked_sizes.end()) continue;
baked_sizes.push_back(size);
for (ImFont* f : {fonts_.menu ? fonts_.menu : fonts_.body, fonts_.heading}) to_bake.push_back({f, px[i]}), kept.push_back({f, px[i]});
}
}
ImTextureData* logo(float* aspect) {
*aspect = logo_aspect;
return logo_;
}
void idle() {
if (!ready) return;
presents++;
reap(false); // (staging buffers and old textures let go while nothing shows too)
}
void record_to(VkCommandBuffer cmd, VkImage image, VkImageView view, uint32_t w, uint32_t h) {
ImGui::Render();
ImDrawData* dd = ImGui::GetDrawData();
presents++;
reap(false);
if (srgb_target) linearize(dd);
else if (dev.hdr) hdr10(dd); // (the overlays at the paper white, not at the display's peak with the wrong primaries)
if (!image || !view) return;
bool textures_ok = true; // (a texture that could not be made has no descriptor: draws that sample it must not be recorded)
if (dd->Textures)
for (ImTextureData* tex : *dd->Textures) {
if (tex->Status == ImTextureStatus_OK || tex->Status == ImTextureStatus_Destroyed) continue;
update_texture(cmd, tex);
if (tex->Status != ImTextureStatus_OK && tex->Status != ImTextureStatus_Destroyed) textures_ok = false;
}
dd->Textures = nullptr; // (done above: the backend's own upload would wait on the queue)
if (!textures_ok) { // (this frame's overlays left out; the texture is asked for again next frame)
static bool told = false;
if (!told) told = true, logf("vk: ui: a texture could not be made (out of memory?): the overlays wait for it");
return;
}
layout(cmd, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT,
VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT);
VkRenderingAttachmentInfo ca{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO};
ca.imageView = view;
ca.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
ca.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
ca.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
VkRenderingInfo ri{VK_STRUCTURE_TYPE_RENDERING_INFO};
ri.renderArea = {{0, 0}, {w, h}};
ri.layerCount = 1;
ri.colorAttachmentCount = 1;
ri.pColorAttachments = &ca;
vkCmdBeginRendering(cmd, &ri);
ImGui_ImplVulkan_RenderDrawData(dd, cmd);
vkCmdEndRendering(cmd);
layout(cmd, image, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT,
VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT);
}
void record(VkCommandBuffer cmd, uint32_t index, uint32_t w, uint32_t h) {
const bool has = index < dev.swap_views.size() && dev.swap_views[index];
record_to(cmd, has ? dev.swap_images[index] : VK_NULL_HANDLE, has ? dev.swap_views[index] : VK_NULL_HANDLE, w, h);
}
} // namespace mkwvk::ui