Subpixel rendering (#186)

Reviewed-on: https://git.madvoxel.net/OpenVulkano/OpenVulkano/pulls/186
Reviewed-by: Georg Hagen <georg.hagen@madvoxel.com>
Co-authored-by: ohyzha <oleksii.hyzha.ext@madvoxel.com>
Co-committed-by: ohyzha <oleksii.hyzha.ext@madvoxel.com>
This commit is contained in:
ohyzha
2025-01-13 11:05:54 +01:00
committed by Oleksii_Hyzha
parent c976d75715
commit f2b164d6e8
20 changed files with 452 additions and 112 deletions

View File

@@ -24,9 +24,10 @@ namespace OpenVulkano::Scene
struct Shelf
{
inline static std::vector<Shelf> CreateShelves(uint32_t atlasWidth, std::vector<GlyphForPacking>& glyphs,
const FtFaceRecPtr& face);
const FtFaceRecPtr& face, int channelsCount);
Shelf(uint32_t width, uint32_t height) : m_width(width), m_height(height), m_remainingWidth(width) {}
Shelf(uint32_t width, uint32_t height, int pixelSize, uint32_t prevShelvesHeight)
: m_width(width), m_height(height), m_remainingWidth(width), m_pixelSize(pixelSize), m_prevShelvesHeight(prevShelvesHeight) {}
bool HasSpaceForGlyph(uint32_t glyphWidth, uint32_t glyphHeight) const
{
return m_remainingWidth >= glyphWidth && m_height >= glyphHeight;
@@ -35,7 +36,10 @@ namespace OpenVulkano::Scene
uint32_t GetHeight() const { return m_height; }
uint32_t GetNextGlyphPos() const { return m_nextGlyphPos; };
uint32_t GetOccupiedSize() const { return ((m_width - m_remainingWidth) * m_height); }
std::optional<uint32_t> AddGlyph(uint32_t glyphWidth, uint32_t glyphHeight)
uint32_t GetPrevShelvesHeight() const { return m_prevShelvesHeight; }
int GetPixelSize() const { return m_pixelSize; }
std::optional<std::pair<uint32_t, uint32_t>> AddGlyph(uint32_t glyphWidth, uint32_t glyphHeight)
{
if (!HasSpaceForGlyph(glyphWidth, glyphHeight))
{
@@ -44,7 +48,13 @@ namespace OpenVulkano::Scene
uint32_t insertionPos = m_nextGlyphPos;
m_nextGlyphPos += glyphWidth;
m_remainingWidth -= glyphWidth;
return insertionPos;
uint32_t hOffset = m_height - (m_height - glyphHeight);
uint32_t glyphFirstByte = (insertionPos * m_pixelSize)
+ ((hOffset * m_width * m_pixelSize) - (m_width * m_pixelSize))
+ (m_prevShelvesHeight * m_width * m_pixelSize);
return std::make_pair(insertionPos, glyphFirstByte);
}
private:
@@ -52,10 +62,12 @@ namespace OpenVulkano::Scene
uint32_t m_height;
uint32_t m_remainingWidth;
uint32_t m_nextGlyphPos = 0;
uint32_t m_prevShelvesHeight;
int m_pixelSize;
};
std::vector<Shelf> Shelf::CreateShelves(uint32_t atlasWidth, std::vector<GlyphForPacking>& glyphs,
const FtFaceRecPtr& face)
const FtFaceRecPtr& face, int channelsCount)
{
std::vector<Shelf> shelves;
for (GlyphForPacking& glyph : glyphs)
@@ -71,10 +83,10 @@ namespace OpenVulkano::Scene
uint32_t totalPrevShelvesHeight = 0;
for (Shelf& shelf : shelves)
{
if (std::optional<uint32_t> insertionPosX = shelf.AddGlyph(glyph.size.x, glyph.size.y))
if (std::optional<std::pair<uint32_t, uint32_t>> opt = shelf.AddGlyph(glyph.size.x, glyph.size.y))
{
glyph.firstGlyphByteInAtlas = *insertionPosX + (totalPrevShelvesHeight * atlasWidth);
glyph.atlasPos.x = *insertionPosX;
glyph.firstGlyphByteInAtlas = opt->second;
glyph.atlasPos.x = opt->first;
glyph.atlasPos.y = totalPrevShelvesHeight;
needNewShelf = false;
break;
@@ -84,9 +96,10 @@ namespace OpenVulkano::Scene
if (needNewShelf)
{
shelves.emplace_back(atlasWidth, glyph.size.y);
shelves.back().AddGlyph(glyph.size.x, glyph.size.y);
glyph.firstGlyphByteInAtlas = totalPrevShelvesHeight * atlasWidth;
shelves.emplace_back(atlasWidth, glyph.size.y, channelsCount, totalPrevShelvesHeight);
Shelf& shelf = shelves.back();
uint32_t firstByte = (*shelf.AddGlyph(glyph.size.x, glyph.size.y)).second;
glyph.firstGlyphByteInAtlas = firstByte;
glyph.atlasPos.x = 0;
glyph.atlasPos.y = totalPrevShelvesHeight;
}