| 16 | SKR_EXTERN_C SKR_RUNTIME_API bool skr_runtime_is_dpi_aware(); |
| 17 | |
| 18 | void create_imgui_resources(SRenderDeviceId render_device, skr::render_graph::RenderGraph* renderGraph, skr_vfs_t* vfs) |
| 19 | { |
| 20 | const auto device = renderGraph->get_backend_device(); |
| 21 | const auto backend = device->adapter->instance->backend; |
| 22 | const auto gfx_queue = renderGraph->get_gfx_queue(); |
| 23 | ImGui::CreateContext(); |
| 24 | ImGuiIO& io = ImGui::GetIO(); |
| 25 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls |
| 26 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking |
| 27 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows |
| 28 | ImGui::StyleColorsDark(); |
| 29 | { |
| 30 | auto& style = ImGui::GetStyle(); |
| 31 | if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 32 | { |
| 33 | style.WindowRounding = 0.0f; |
| 34 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
| 35 | } |
| 36 | const char8_t* font_path = u8"./../resources/font/SourceSansPro-Regular.ttf"; |
| 37 | uint32_t *font_bytes, font_length; |
| 38 | read_bytes(font_path, &font_bytes, &font_length); |
| 39 | float dpi_scaling = 1.f; |
| 40 | if (!skr_runtime_is_dpi_aware()) |
| 41 | { |
| 42 | float ddpi; |
| 43 | SDL_GetDisplayDPI(0, &ddpi, NULL, NULL); |
| 44 | dpi_scaling = ddpi / OS_DPI; |
| 45 | // scale back |
| 46 | style.ScaleAllSizes(1.f / dpi_scaling); |
| 47 | ImGui::GetIO().FontGlobalScale = 1.f / dpi_scaling; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | float ddpi; |
| 52 | SDL_GetDisplayDPI(0, &ddpi, NULL, NULL); |
| 53 | dpi_scaling = ddpi / OS_DPI; |
| 54 | // scale back |
| 55 | style.ScaleAllSizes(dpi_scaling); |
| 56 | } |
| 57 | ImFontConfig cfg = {}; |
| 58 | cfg.SizePixels = 16.f * dpi_scaling; |
| 59 | cfg.OversampleH = cfg.OversampleV = 1; |
| 60 | cfg.PixelSnapH = true; |
| 61 | ImGui::GetIO().Fonts->AddFontFromMemoryTTF(font_bytes, |
| 62 | font_length, cfg.SizePixels, &cfg); |
| 63 | ImGui::GetIO().Fonts->Build(); |
| 64 | free(font_bytes); |
| 65 | } |
| 66 | skr::String vsname = u8"shaders/imgui_vertex"; |
| 67 | skr::String fsname = u8"shaders/imgui_fragment"; |
| 68 | vsname.append(backend == ::CGPU_BACKEND_D3D12 ? u8".dxil" : u8".spv"); |
| 69 | fsname.append(backend == ::CGPU_BACKEND_D3D12 ? u8".dxil" : u8".spv"); |
| 70 | auto vsfile = skr_vfs_fopen(vfs, vsname.u8_str(), SKR_FM_READ_BINARY, SKR_FILE_CREATION_OPEN_EXISTING); |
| 71 | uint32_t im_vs_length = (uint32_t)skr_vfs_fsize(vsfile); |
| 72 | uint32_t* im_vs_bytes = (uint32_t*)sakura_malloc(im_vs_length); |
| 73 | skr_vfs_fread(vsfile, im_vs_bytes, 0, im_vs_length); |
| 74 | skr_vfs_fclose(vsfile); |
| 75 | auto fsfile = skr_vfs_fopen(vfs, fsname.u8_str(), SKR_FM_READ_BINARY, SKR_FILE_CREATION_OPEN_EXISTING); |
no test coverage detected