Files
Triforce-Source/gpu/vk/vk_imgui.cpp
ruckus7382sfgsgdf 6f338c8f56 Triforce v0.5.0
In-game mods (GameBanana browser, search and installs, with the Mask Salesman), code-patch and cheat mods, the devs' test18 to test45 work (full performance overlay, frame pacing on, steady frame rate, save export and import, bug capture, new options rows, Setup's Mods page and Add to Steam), occlusion queries and new defaults, Auto resolution that scales, replay helper threads and robust buffers off by default, the options in sections.
2026-11-17 22:40:50 -06:00

627 lines
33 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 <filesystem> // ((Triforce) folder_files: the Debug tab's name pages)
#include <mutex>
#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;
static std::mutex mu; // ((Triforce) the button icons are read on a thread of their own: load_glyphs)
std::lock_guard lk(mu);
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. Read and unpacked on a
// thread of its own (read_glyphs: on the presenting thread, at the menu's first frame, it was part of what held the first open
// of the options), made a font of the atlas at the next begin (load_glyphs). The reader's dictionary is read-only, shared
// with the menu fonts' reads ----
std::atomic<int> glyphs_state{0}; // (0 not asked, 1 being read, 2 read, 3 made a font or found none)
std::vector<unsigned char> glyph_bytes; // (read_glyphs's, until load_glyphs takes them: glyphs_state orders the two)
const char* glyph_from = "the game's own";
void read_glyphs() {
std::vector<unsigned char> font;
const char* from = "the game's own";
// (host/prompts.c: prompts/External.bfotf.zs, no dictionary. (Triforce) Not the keyboard and mouse face's: these icons
// show while a controller is the menu's last input, and that face's are keys; the menu draws its own caps for the keys)
if (host && host->prompt_face && host->prompt_face() >= 0 && host->prompt_face() != HOS_PROMPT_KEYBOARD) {
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);
}
glyph_bytes = std::move(font), glyph_from = from;
glyphs_state.store(2, std::memory_order_release);
}
void load_glyphs() {
glyphs_state.store(3, std::memory_order_relaxed);
if (glyph_bytes.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(glyph_bytes, "External");
std::vector<unsigned char>().swap(glyph_bytes);
logf("vk: ui: the button icons: %s", glyph_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;
}
/// (Triforce, 11-13, the user: "Why is the [shader loading screen] so dark for me after turning hdr on?") A texture's texel
/// for an HDR10 window image. The pipeline multiplies a texel by its vertex's colour, and hdr10 below codes every vertex
/// colour, a picture's white tint too: white at the paper white is PQ code 0.65 at 400 nits (0.58 at 200), not 1. A texel
/// coded the same way came out as code * 0.65: a white texel at 0.42, about 40 nits for 400, and darker the dimmer it was.
/// Text and plain shapes were right (their texel is white coverage; the colour is the vertex's alone), every picture was ten
/// times too dark: the start-up screen's crest and its stones, the logo, the plates of the options inside the game's own
/// screen. So a texel is its HDR10 code over the code of white: times a white tint it is its own code again, exactly, and a
/// white shape under a coloured tint is the tint's code, exactly (a coloured texel under a coloured tint is still a
/// product of two codes: close, not exact; the exact way is the conversion in the fragment shader, not done)
ImU32 hdr10_texel(ImU32 c, float white) {
static unsigned char over_white[256];
static float made_for = -1;
if (made_for != white) {
const float w = (float)(hdr10_of(IM_COL32_WHITE, white) >> IM_COL32_R_SHIFT & 0xFF);
for (int i = 0; i < 256; i++) over_white[i] = (unsigned char)std::clamp((int)(i * 255.0f / std::max(w, 1.0f) + 0.5f), 0, 255);
made_for = white;
}
const ImU32 v = hdr10_of(c, white);
return (v & IM_COL32_A_MASK) | (ImU32)over_white[v >> IM_COL32_R_SHIFT & 0xFF] << IM_COL32_R_SHIFT |
(ImU32)over_white[v >> IM_COL32_G_SHIFT & 0xFF] << IM_COL32_G_SHIFT | (ImU32)over_white[v >> IM_COL32_B_SHIFT & 0xFF] << 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);
// (over white's code: see hdr10_texel. RX_VK_HDR_TEXEL_OLD=1, a lab switch: coded as a colour, as before)
static const bool as_before = getenv("RX_VK_HDR_TEXEL_OLD") != nullptr;
c = as_before ? hdr10_of(c, white) : hdr10_texel(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;
static int fails = 0; // ((Triforce) why a texture could not be made, the first few times)
auto why = [&](const char* step) {
if (++fails <= 8) logf("vk: ui: texture %dx%d (status %d, %s) not made: %s", tex->Width, tex->Height, (int)tex->Status,
create ? "new" : "an update", step);
};
if (!size) return why("no size");
if (!create_buffer(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, buf, buf_alloc, &map)) return why("its staging buffer");
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)
why(!made.image ? "its image" : !made.view ? "its view" : "its descriptor set");
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)
// (the backend's own pool: the textures' descriptor sets: the glyphs, the logo, the game's menu skin; (Triforce) the Mods
// screen's pictures and the Salesman. 64 ran out at the fifth page of mods, 11-17: every overlay then waited on it)
ii.DescriptorPoolSize = 1024;
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_state.load(std::memory_order_acquire) == 2) 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() {
int none = 0;
if (glyphs_state.compare_exchange_strong(none, 1)) std::thread(read_glyphs).detach();
}
std::vector<std::string> folder_files(const std::string& dir, const char* ending) {
std::vector<std::string> names;
const size_t len = strlen(ending);
std::error_code ec;
const std::filesystem::path path(std::u8string((const char8_t*)dir.data(), dir.size()));
for (std::filesystem::directory_iterator it(path, ec), end; !ec && it != end; it.increment(ec)) {
if (!it->is_regular_file(ec)) continue;
const std::u8string u8 = it->path().filename().u8string();
std::string name((const char*)u8.data(), u8.size());
if (name.size() <= len) continue;
bool same = true; // (the ending whatever its case: .BIN is .bin)
for (size_t i = 0; same && i < len; i++) same = tolower((unsigned char)name[name.size() - len + i]) == tolower((unsigned char)ending[i]);
if (same) names.push_back(std::move(name));
}
std::sort(names.begin(), names.end());
return names;
}
bool game_files(const char* folder, const char* ending,
const std::function<void(const std::string& name, const std::vector<unsigned char>& bytes)>& each) {
game_font::Reader reader; // (its own: the fonts' is the presenting thread's)
if (!reader.open(romfs_dir())) return false;
std::vector<unsigned char> bytes;
for (const std::string& name : folder_files(reader.romfs + folder, ending))
if (reader.read_packed((std::string(folder) + "/" + name).c_str(), bytes)) each(name, bytes);
return 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;
// (a texture that could not be made has no descriptor: draws that sample it must not be recorded. (Triforce, 11-17) Those
// draws alone are left out, not the frame's overlays: one picture of the Mods screen that could not be made hid every
// overlay, the menus and the Mods entry under the game's list with them. The font atlas missing leaves the frame out)
ImVector<ImTextureData*> missing;
bool atlas_missing = false;
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) {
missing.push_back(tex);
atlas_missing |= tex == ImGui::GetIO().Fonts->TexData;
}
}
dd->Textures = nullptr; // (done above: the backend's own upload would wait on the queue)
if (!missing.empty()) { // (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?): %s", atlas_missing ? "the overlays wait for it" : "its pictures left out");
if (atlas_missing) return;
for (ImDrawList* list : dd->CmdLists)
for (ImDrawCmd& dc : list->CmdBuffer)
if (dc.TexRef._TexData && missing.contains(dc.TexRef._TexData))
dc.ElemCount = 0, dc.TexRef = ImGui::GetIO().Fonts->TexRef; // (nothing drawn; a descriptor that is there bound)
}
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