Initial SDF font generation work.

This commit is contained in:
Skyth 2024-12-10 19:16:20 +03:00
parent 632cf2619e
commit dd013580a0
9 changed files with 140 additions and 41 deletions

3
.gitmodules vendored
View file

@ -13,3 +13,6 @@
[submodule "UnleashedRecompResources"] [submodule "UnleashedRecompResources"]
path = UnleashedRecompResources path = UnleashedRecompResources
url = https://github.com/hedge-dev/UnleashedRecompResources.git url = https://github.com/hedge-dev/UnleashedRecompResources.git
[submodule "thirdparty/msdf-atlas-gen"]
path = thirdparty/msdf-atlas-gen
url = https://github.com/Chlumsky/msdf-atlas-gen.git

View file

@ -88,6 +88,7 @@ set(SWA_CPU_CXX_SOURCES
set(SWA_GPU_CXX_SOURCES set(SWA_GPU_CXX_SOURCES
"gpu/video.cpp" "gpu/video.cpp"
"gpu/imgui_common.cpp" "gpu/imgui_common.cpp"
"gpu/imgui_font_builder.cpp"
"gpu/imgui_snapshot.cpp" "gpu/imgui_snapshot.cpp"
"gpu/rhi/plume_d3d12.cpp" "gpu/rhi/plume_d3d12.cpp"
"gpu/rhi/plume_vulkan.cpp" "gpu/rhi/plume_vulkan.cpp"
@ -258,6 +259,7 @@ target_link_libraries(UnleashedRecomp PRIVATE
magic_enum::magic_enum magic_enum::magic_enum
unofficial::tiny-aes-c::tiny-aes-c unofficial::tiny-aes-c::tiny-aes-c
nfd::nfd nfd::nfd
msdf-atlas-gen::msdf-atlas-gen
) )
target_include_directories(UnleashedRecomp PRIVATE target_include_directories(UnleashedRecomp PRIVATE

View file

@ -0,0 +1,86 @@
#include "imgui_font_builder.h"
#include <msdf-atlas-gen/msdf-atlas-gen.h>
static bool FontBuilder_Build(ImFontAtlas* atlas)
{
auto freeType = msdfgen::initializeFreetype();
std::vector<msdf_atlas::GlyphGeometry> glyphs;
std::vector<std::pair<size_t, size_t>> ranges;
for (auto& configData : atlas->ConfigData)
{
msdf_atlas::Charset charset;
const ImWchar* glyphRanges = configData.GlyphRanges;
while (*glyphRanges != NULL)
{
for (ImWchar i = glyphRanges[0]; i <= glyphRanges[1]; i++)
charset.add(i);
glyphRanges += 2;
}
size_t index = glyphs.size();
auto font = msdfgen::loadFontData(freeType, reinterpret_cast<const msdfgen::byte*>(configData.FontData), configData.FontDataSize);
msdf_atlas::FontGeometry fontGeometry(&glyphs);
fontGeometry.loadCharset(font, 1.0, charset);
msdfgen::destroyFont(font);
ranges.emplace_back(index, glyphs.size() - index);
}
for (auto& glyph : glyphs)
glyph.edgeColoring(&msdfgen::edgeColoringInkTrap, 3.0, 0);
msdf_atlas::TightAtlasPacker packer;
packer.setMinimumScale(32.0);
packer.pack(glyphs.data(), glyphs.size());
int width = 0, height = 0;
packer.getDimensions(width, height);
msdf_atlas::ImmediateAtlasGenerator<float, 4, &msdf_atlas::mtsdfGenerator, msdf_atlas::BitmapAtlasStorage<uint8_t, 4>> generator(width, height);
generator.generate(glyphs.data(), glyphs.size());
for (size_t i = 0; i < atlas->ConfigData.size(); i++)
{
auto& config = atlas->ConfigData[i];
config.DstFont->FontSize = config.SizePixels;
config.DstFont->ConfigData = &config;
config.DstFont->ConfigDataCount = 1;
config.DstFont->ContainerAtlas = atlas;
// TODO: ascent? descent? wat do they mean
auto& [index, count] = ranges[i];
for (size_t j = 0; j < count; j++)
{
auto& glyph = glyphs[index + j];
double x0, y0, x1, y1, u0, v0, u1, v1;
glyph.getQuadPlaneBounds(x0, y0, x1, y1);
glyph.getQuadAtlasBounds(u0, v0, u1, v1);
config.DstFont->AddGlyph(&config, glyph.getCodepoint(), x0, y0, x1, y1, u0, v0, u1, v1, glyph.getAdvance());
}
config.DstFont->BuildLookupTable();
}
atlas->TexReady = true;
atlas->TexPixelsUseColors = true;
atlas->TexPixelsRGBA32 = (unsigned int*)IM_ALLOC(width * height * 4);
atlas->TexWidth = width;
atlas->TexHeight = height;
atlas->TexUvScale = { 1.0f / width, 1.0f / height };
auto bitmapRef = (msdfgen::BitmapConstRef<uint8_t, 4>)generator.atlasStorage();
memcpy(atlas->TexPixelsRGBA32, bitmapRef.pixels, width * height * 4);
msdfgen::deinitializeFreetype(freeType);
return true;
}
ImFontBuilderIO g_fontBuilderIO = { FontBuilder_Build };

View file

@ -0,0 +1,3 @@
#pragma once
extern ImFontBuilderIO g_fontBuilderIO;

View file

@ -34,8 +34,7 @@ struct ImDrawDataSnapshot
// Undefine this to generate a font atlas file in working directory. // Undefine this to generate a font atlas file in working directory.
// You also need to do this if you are testing localization, as only // You also need to do this if you are testing localization, as only
// characters in the locale get added to the atlas. // characters in the locale get added to the atlas.
// Don't forget to compress the generated atlas texture to BC4 with no mips! //#define ENABLE_IM_FONT_ATLAS_SNAPSHOT
#define ENABLE_IM_FONT_ATLAS_SNAPSHOT
struct ImFontAtlasSnapshot struct ImFontAtlasSnapshot
{ {

View file

@ -1,35 +1,37 @@
#include <stdafx.h> #include "video.h"
#include <kernel/function.h> #include "imgui_common.h"
#include <kernel/heap.h> #include "imgui_snapshot.h"
#include "imgui_font_builder.h"
#include <bc_diff.h>
#include <cpu/code_cache.h> #include <cpu/code_cache.h>
#include <cpu/guest_code.h> #include <cpu/guest_code.h>
#include <cpu/guest_thread.h> #include <cpu/guest_thread.h>
#include <decompressor.h>
#include <kernel/function.h>
#include <kernel/heap.h>
#include <kernel/memory.h> #include <kernel/memory.h>
#include <kernel/xdbf.h> #include <kernel/xdbf.h>
#include <xxHashMap.h> #include <res/bc_diff/button_bc_diff.bin.h>
#include <res/font/im_font_atlas.dds.h>
#include <shader/shader_cache.h> #include <shader/shader_cache.h>
#include <SWA.h>
#include <ui/achievement_menu.h> #include <ui/achievement_menu.h>
#include <ui/achievement_overlay.h> #include <ui/achievement_overlay.h>
#include <ui/button_guide.h> #include <ui/button_guide.h>
#include <ui/fader.h> #include <ui/fader.h>
#include <ui/installer_wizard.h>
#include <ui/message_window.h> #include <ui/message_window.h>
#include <ui/options_menu.h> #include <ui/options_menu.h>
#include <ui/installer_wizard.h>
#include "imgui_snapshot.h"
#include "imgui_common.h"
#include "video.h"
#include <ui/sdl_listener.h> #include <ui/sdl_listener.h>
#include <ui/window.h> #include <ui/window.h>
#include <user/config.h> #include <user/config.h>
#include <xxHashMap.h>
#include <res/font/im_font_atlas.dds.h> #if defined(ASYNC_PSO_DEBUG) || defined(PSO_CACHING)
#include <res/bc_diff/button_bc_diff.bin.h> #include <magic_enum.hpp>
#include <decompressor.h> #endif
#include <bc_diff.h>
#include <SWA.h>
#include "../../thirdparty/ShaderRecomp/ShaderRecomp/shader_common.h" #include "../../thirdparty/ShaderRecomp/ShaderRecomp/shader_common.h"
#include "shader/copy_vs.hlsl.dxil.h" #include "shader/copy_vs.hlsl.dxil.h"
@ -38,6 +40,8 @@
#include "shader/csd_filter_ps.hlsl.spirv.h" #include "shader/csd_filter_ps.hlsl.spirv.h"
#include "shader/enhanced_motion_blur_ps.hlsl.dxil.h" #include "shader/enhanced_motion_blur_ps.hlsl.dxil.h"
#include "shader/enhanced_motion_blur_ps.hlsl.spirv.h" #include "shader/enhanced_motion_blur_ps.hlsl.spirv.h"
#include "shader/gamma_correction_ps.hlsl.dxil.h"
#include "shader/gamma_correction_ps.hlsl.spirv.h"
#include "shader/gaussian_blur_3x3.hlsl.dxil.h" #include "shader/gaussian_blur_3x3.hlsl.dxil.h"
#include "shader/gaussian_blur_3x3.hlsl.spirv.h" #include "shader/gaussian_blur_3x3.hlsl.spirv.h"
#include "shader/gaussian_blur_5x5.hlsl.dxil.h" #include "shader/gaussian_blur_5x5.hlsl.dxil.h"
@ -46,16 +50,14 @@
#include "shader/gaussian_blur_7x7.hlsl.spirv.h" #include "shader/gaussian_blur_7x7.hlsl.spirv.h"
#include "shader/gaussian_blur_9x9.hlsl.dxil.h" #include "shader/gaussian_blur_9x9.hlsl.dxil.h"
#include "shader/gaussian_blur_9x9.hlsl.spirv.h" #include "shader/gaussian_blur_9x9.hlsl.spirv.h"
#include "shader/gamma_correction_ps.hlsl.dxil.h"
#include "shader/gamma_correction_ps.hlsl.spirv.h"
#include "shader/imgui_ps.hlsl.dxil.h" #include "shader/imgui_ps.hlsl.dxil.h"
#include "shader/imgui_ps.hlsl.spirv.h" #include "shader/imgui_ps.hlsl.spirv.h"
#include "shader/imgui_vs.hlsl.dxil.h" #include "shader/imgui_vs.hlsl.dxil.h"
#include "shader/imgui_vs.hlsl.spirv.h" #include "shader/imgui_vs.hlsl.spirv.h"
#include "shader/movie_vs.hlsl.dxil.h"
#include "shader/movie_vs.hlsl.spirv.h"
#include "shader/movie_ps.hlsl.dxil.h" #include "shader/movie_ps.hlsl.dxil.h"
#include "shader/movie_ps.hlsl.spirv.h" #include "shader/movie_ps.hlsl.spirv.h"
#include "shader/movie_vs.hlsl.dxil.h"
#include "shader/movie_vs.hlsl.spirv.h"
#include "shader/resolve_msaa_depth_2x.hlsl.dxil.h" #include "shader/resolve_msaa_depth_2x.hlsl.dxil.h"
#include "shader/resolve_msaa_depth_2x.hlsl.spirv.h" #include "shader/resolve_msaa_depth_2x.hlsl.spirv.h"
#include "shader/resolve_msaa_depth_4x.hlsl.dxil.h" #include "shader/resolve_msaa_depth_4x.hlsl.dxil.h"
@ -63,10 +65,6 @@
#include "shader/resolve_msaa_depth_8x.hlsl.dxil.h" #include "shader/resolve_msaa_depth_8x.hlsl.dxil.h"
#include "shader/resolve_msaa_depth_8x.hlsl.spirv.h" #include "shader/resolve_msaa_depth_8x.hlsl.spirv.h"
#if defined(ASYNC_PSO_DEBUG) || defined(PSO_CACHING)
#include <magic_enum.hpp>
#endif
extern "C" extern "C"
{ {
__declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001; __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
@ -1112,6 +1110,8 @@ struct ImGuiPushConstants
ImVec2 scale{ 1.0f, 1.0f }; ImVec2 scale{ 1.0f, 1.0f };
}; };
extern ImFontBuilderIO g_fontBuilderIO;
static void CreateImGuiBackend() static void CreateImGuiBackend()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
@ -1123,8 +1123,8 @@ static void CreateImGuiBackend()
IM_DELETE(io.Fonts); IM_DELETE(io.Fonts);
io.Fonts = ImFontAtlasSnapshot::Load(); io.Fonts = ImFontAtlasSnapshot::Load();
#else #else
io.Fonts->TexDesiredWidth = 4096;
io.Fonts->AddFontDefault(); io.Fonts->AddFontDefault();
io.Fonts->FontBuilderIO = &g_fontBuilderIO;
ImFontAtlasSnapshot::GenerateGlyphRanges(); ImFontAtlasSnapshot::GenerateGlyphRanges();
#endif #endif
@ -1137,17 +1137,16 @@ static void CreateImGuiBackend()
ImGui_ImplSDL2_InitForOther(Window::s_pWindow); ImGui_ImplSDL2_InitForOther(Window::s_pWindow);
RenderComponentMapping componentMapping(RenderSwizzle::ONE, RenderSwizzle::ONE, RenderSwizzle::ONE, RenderSwizzle::R);
#ifdef ENABLE_IM_FONT_ATLAS_SNAPSHOT #ifdef ENABLE_IM_FONT_ATLAS_SNAPSHOT
g_imFontTexture = LoadTexture(decompressZstd(g_im_font_atlas_texture, g_im_font_atlas_texture_uncompressed_size).get(), g_imFontTexture = LoadTexture(
g_im_font_atlas_texture_uncompressed_size, componentMapping); decompressZstd(g_im_font_atlas_texture, g_im_font_atlas_texture_uncompressed_size).get(), g_im_font_atlas_texture_uncompressed_size);
#else #else
g_imFontTexture = std::make_unique<GuestTexture>(ResourceType::Texture); g_imFontTexture = std::make_unique<GuestTexture>(ResourceType::Texture);
io.Fonts->Build();
uint8_t* pixels; uint8_t* pixels;
int width, height; int width, height;
io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height); io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
RenderTextureDesc textureDesc; RenderTextureDesc textureDesc;
textureDesc.dimension = RenderTextureDimension::TEXTURE_2D; textureDesc.dimension = RenderTextureDimension::TEXTURE_2D;
@ -1156,17 +1155,17 @@ static void CreateImGuiBackend()
textureDesc.depth = 1; textureDesc.depth = 1;
textureDesc.mipLevels = 1; textureDesc.mipLevels = 1;
textureDesc.arraySize = 1; textureDesc.arraySize = 1;
textureDesc.format = RenderFormat::R8_UNORM; textureDesc.format = RenderFormat::R8G8B8A8_UNORM;
g_imFontTexture->textureHolder = g_device->createTexture(textureDesc); g_imFontTexture->textureHolder = g_device->createTexture(textureDesc);
g_imFontTexture->texture = g_imFontTexture->textureHolder.get(); g_imFontTexture->texture = g_imFontTexture->textureHolder.get();
uint32_t rowPitch = (width + PITCH_ALIGNMENT - 1) & ~(PITCH_ALIGNMENT - 1); uint32_t rowPitch = (width * 4 + PITCH_ALIGNMENT - 1) & ~(PITCH_ALIGNMENT - 1);
uint32_t slicePitch = (rowPitch * height + PLACEMENT_ALIGNMENT - 1) & ~(PLACEMENT_ALIGNMENT - 1); uint32_t slicePitch = (rowPitch * height + PLACEMENT_ALIGNMENT - 1) & ~(PLACEMENT_ALIGNMENT - 1);
auto uploadBuffer = g_device->createBuffer(RenderBufferDesc::UploadBuffer(slicePitch)); auto uploadBuffer = g_device->createBuffer(RenderBufferDesc::UploadBuffer(slicePitch));
uint8_t* mappedMemory = reinterpret_cast<uint8_t*>(uploadBuffer->map()); uint8_t* mappedMemory = reinterpret_cast<uint8_t*>(uploadBuffer->map());
if (rowPitch == width) if (rowPitch == (width * 4))
{ {
memcpy(mappedMemory, pixels, slicePitch); memcpy(mappedMemory, pixels, slicePitch);
} }
@ -1174,8 +1173,8 @@ static void CreateImGuiBackend()
{ {
for (size_t i = 0; i < height; i++) for (size_t i = 0; i < height; i++)
{ {
memcpy(mappedMemory, pixels, width); memcpy(mappedMemory, pixels, width * 4);
pixels += width; pixels += width * 4;
mappedMemory += rowPitch; mappedMemory += rowPitch;
} }
} }
@ -1188,7 +1187,7 @@ static void CreateImGuiBackend()
g_copyCommandList->copyTextureRegion( g_copyCommandList->copyTextureRegion(
RenderTextureCopyLocation::Subresource(g_imFontTexture->texture, 0), RenderTextureCopyLocation::Subresource(g_imFontTexture->texture, 0),
RenderTextureCopyLocation::PlacedFootprint(uploadBuffer.get(), RenderFormat::R8_UNORM, width, height, 1, rowPitch, 0)); RenderTextureCopyLocation::PlacedFootprint(uploadBuffer.get(), RenderFormat::R8G8B8A8_UNORM, width, height, 1, rowPitch / 4, 0));
}); });
g_imFontTexture->layout = RenderTextureLayout::COPY_DEST; g_imFontTexture->layout = RenderTextureLayout::COPY_DEST;
@ -1197,7 +1196,6 @@ static void CreateImGuiBackend()
textureViewDesc.format = textureDesc.format; textureViewDesc.format = textureDesc.format;
textureViewDesc.dimension = RenderTextureViewDimension::TEXTURE_2D; textureViewDesc.dimension = RenderTextureViewDimension::TEXTURE_2D;
textureViewDesc.mipLevels = 1; textureViewDesc.mipLevels = 1;
textureViewDesc.componentMapping = componentMapping;
g_imFontTexture->textureView = g_imFontTexture->texture->createTextureView(textureViewDesc); g_imFontTexture->textureView = g_imFontTexture->texture->createTextureView(textureViewDesc);
g_imFontTexture->descriptorIndex = g_textureDescriptorAllocator.allocate(); g_imFontTexture->descriptorIndex = g_textureDescriptorAllocator.allocate();
@ -1261,7 +1259,7 @@ static void CreateImGuiBackend()
ddspp::Header header; ddspp::Header header;
ddspp::HeaderDXT10 headerDX10; ddspp::HeaderDXT10 headerDX10;
ddspp::encode_header(ddspp::R8_UNORM, width, height, 1, ddspp::Texture2D, 1, 1, header, headerDX10); ddspp::encode_header(ddspp::R8G8B8A8_UNORM, width, height, 1, ddspp::Texture2D, 1, 1, header, headerDX10);
file = fopen("im_font_atlas.dds", "wb"); file = fopen("im_font_atlas.dds", "wb");
if (file) if (file)
@ -1269,7 +1267,7 @@ static void CreateImGuiBackend()
fwrite(&ddspp::DDS_MAGIC, 4, 1, file); fwrite(&ddspp::DDS_MAGIC, 4, 1, file);
fwrite(&header, sizeof(header), 1, file); fwrite(&header, sizeof(header), 1, file);
fwrite(&headerDX10, sizeof(headerDX10), 1, file); fwrite(&headerDX10, sizeof(headerDX10), 1, file);
fwrite(pixels, 1, width * height, file); fwrite(pixels, 4, width * height, file);
fclose(file); fclose(file);
} }
#endif #endif

View file

@ -1,4 +1,10 @@
set(MSDF_ATLAS_BUILD_STANDALONE OFF)
set(MSDF_ATLAS_USE_SKIA OFF)
set(MSDF_ATLAS_NO_ARTERY_FONT ON)
set(MSDFGEN_DISABLE_PNG ON)
add_subdirectory(${SWA_THIRDPARTY_ROOT}/PowerRecomp) add_subdirectory(${SWA_THIRDPARTY_ROOT}/PowerRecomp)
add_subdirectory(${SWA_THIRDPARTY_ROOT}/ShaderRecomp) add_subdirectory(${SWA_THIRDPARTY_ROOT}/ShaderRecomp)
add_subdirectory(${SWA_THIRDPARTY_ROOT}/o1heap) add_subdirectory(${SWA_THIRDPARTY_ROOT}/o1heap)
add_subdirectory(${SWA_THIRDPARTY_ROOT}/fshasher) add_subdirectory(${SWA_THIRDPARTY_ROOT}/fshasher)
add_subdirectory(${SWA_THIRDPARTY_ROOT}/msdf-atlas-gen)

1
thirdparty/msdf-atlas-gen vendored Submodule

@ -0,0 +1 @@
Subproject commit 7e8d34645a67c6e4def5d281f7adec1c2501dc49

View file

@ -23,6 +23,7 @@
}, },
"magic-enum", "magic-enum",
"nativefiledialog-extended", "nativefiledialog-extended",
"miniaudio" "miniaudio",
"freetype"
] ]
} }