What's the correct way to upload the msdf generated atlas to GPU using Vulkan? #44
-
I am trying to integrate msdf-atlas-gen into my engine to render texts in the scene and I have come across a weird issue when uploading the textures in Vulkan. The image is flipped vertically + there are some differences between the location of fonts. I am using the same snippet as the one where the whole atlas is generated: std::vector<GlyphGeometry> msdfGlyphs;
FontGeometry fontGeometry(&msdfGlyphs);
fontGeometry.loadCharset(font, 1.0, Charset::ASCII);
for (auto &glyph : msdfGlyphs) {
glyph.edgeColoring(&msdfgen::edgeColoringInkTrap, MAX_CORNER_ANGLE, 0);
}
TightAtlasPacker packer;
packer.setDimensionsConstraint(
TightAtlasPacker::DimensionsConstraint::SQUARE);
packer.setMinimumScale(MINIMUM_SCALE);
packer.setPixelRange(PIXEL_RANGE);
packer.setMiterLimit(1.0);
packer.pack(msdfGlyphs.data(), static_cast<int>(msdfGlyphs.size()));
int width = 0, height = 0;
packer.getDimensions(width, height);
ImmediateAtlasGenerator<float, 4, &mtsdfGenerator,
BitmapAtlasStorage<byte, 4>>
generator(width, height);
GeneratorAttributes attributes;
generator.setAttributes(attributes);
generator.setThreadCount(1);
generator.generate(msdfGlyphs.data(), static_cast<int>(msdfGlyphs.size()));
std::unordered_map<uint32_t, FontGlyph> glyphs;
auto fWidth = static_cast<float>(width);
auto fHeight = static_cast<float>(height);
// my custom asset type for fonts
FontAsset fontAsset{};
fontAsset.msdfGlyphs = msdfGlyphs;
fontAsset.msdfAtlas = generator.atlasStorage();
return assetManager.addFont(fontAsset); After MSDF fonts are loaded, I upload them to my the render device (using Vulkan): rhi::TextureDescription description{};
msdfgen::BitmapRef<msdf_atlas::byte, 4> ref = font.data.msdfAtlas;
description.data = ref.pixels;
description.width = ref.width;
description.height = ref.height;
description.usage = rhi::TextureUsage::Color |
rhi::TextureUsage::TransferDestination |
rhi::TextureUsage::Sampled;
description.format = VK_FORMAT_R8G8B8A8_UNORM;
description.size = sizeof(msdf_atlas::byte) * 4 * ref.width * ref.height;
auto textureHandle = deviceRegistry.setTexture(description); Expected (generated using msdf-atlas-gen application for comparison): Actual (captured from RenderDoc): I opened Windows Image Viewer and flipped the image vertically and there are differences between the order of characters (e.g first row last column is "W" instead of "A"): At last, I am using MTSDF in my codebase (I am using What am I doing wrong here? When I upload the texture data to the GPU, I just copy the raw texture data without any modifications (I have tested it with regular images such as PNG); so, from my understanding, this is something that I need to handle during atlas generation. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Hey, I see that you put the pixels into a |
Beta Was this translation helpful? Give feedback.
msdf-atlas-gen
represents bitmaps as starting from the bottom row, which is the case for OpenGL, but not Vulkan.msdfgen::savePng
after generating your image.