_font drawcall opt

This commit is contained in:
2023-09-09 13:41:13 +03:00
parent 26ae90a999
commit 674d5b9709
3 changed files with 161 additions and 145 deletions

View File

@@ -233,9 +233,11 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
if (lastMoveLine)
break;
}
}
characterEntries.Add(entry);
characterKernings.Add((float)kerning * scale);
}
// Check if move to another line
if (moveLine)
@@ -266,7 +268,7 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
// Add line
tmpLine.Size.X = cursorX;
tmpLine.Size.Y = baseLinesDistance;
tmpLine.LastCharIndex = textLength - 1;
tmpLine.LastCharIndex = textLength;
outputLines.Add(tmpLine);
tmpLine.Location.Y += baseLinesDistance;

View File

@@ -1363,9 +1363,14 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
characterKernings.Clear();
font->ProcessText(text, Lines, layout, characterEntries, characterKernings);
auto drawCallCountBefore = DrawCalls.Count();
DrawCalls.EnsureCapacity(drawCallCountBefore + characterEntries.Count());
DrawCalls.Resize(drawCallCountBefore + characterEntries.Count());
Render2DDrawCall* drawCall = &DrawCalls[drawCallCountBefore];
// Render all lines
//FontCharacterEntry entry;
Render2DDrawCall drawCall;
/*Render2DDrawCall drawCall;
if (customMaterial)
{
drawCall.Type = DrawCallType::DrawCharMaterial;
@@ -1375,51 +1380,28 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
{
drawCall.Type = DrawCallType::DrawChar;
drawCall.AsChar.Mat = nullptr;
}
}*/
int32 entryIndex = 0;
int32 actualDrawCallsCount = 0;
for (int32 lineIndex = 0; lineIndex < Lines.Count(); lineIndex++)
{
const FontLineCache& line = Lines[lineIndex];
Float2 pointer = line.Location;
// Render all characters from the line
for (int32 charIndex = line.FirstCharIndex; charIndex <= line.LastCharIndex; charIndex++)
for (int32 charIndex = line.FirstCharIndex; charIndex < line.LastCharIndex; charIndex++)
{
// Cache current character
const Char currentChar = text[charIndex];
const Char c = text[charIndex];
// Check if it isn't a newline character
if (currentChar != '\n')
{
// Get character entry
//font->GetCharacter(currentChar, entry, enableFallbackFonts);
//font->GetCharacter(c, entry);
const FontCharacterEntry& entry = characterEntries[entryIndex];
float kerning = characterKernings[entryIndex];
// Check if need to select/change font atlas (since characters even in the same font may be located in different atlases)
if (fontAtlas == nullptr || entry.TextureIndex != fontAtlasIndex)
{
// Get texture atlas that contains current character
fontAtlasIndex = entry.TextureIndex;
fontAtlas = FontManager::GetAtlas(fontAtlasIndex);
if (fontAtlas)
{
fontAtlas->EnsureTextureCreated();
invAtlasSize = 1.0f / fontAtlas->GetSize();
drawCall.AsChar.Tex = fontAtlas->GetTexture();
}
else
{
invAtlasSize = 1.0f;
drawCall.AsChar.Tex = nullptr;
}
}
// Get kerning
const bool isWhitespace = StringUtils::IsWhitespace(currentChar);
const bool isWhitespace = StringUtils::IsWhitespace(c); // kerning == inf;
/*if (!isWhitespace && previous.IsValid)
{
kerning = entry.Font->GetKerning(previous.Character, entry.Character);
kerning = font->GetKerning(previous.Character, entry.Character);
}
else
{
@@ -1434,6 +1416,25 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
if (!isWhitespace)
//if (xAdvance != 0.0f)
{
// Check if need to select/change font atlas (since characters even in the same font may be located in different atlases)
if (fontAtlas == nullptr || entry.TextureIndex != fontAtlasIndex)
{
// Get texture atlas that contains current character
fontAtlasIndex = entry.TextureIndex;
fontAtlas = FontManager::GetAtlas(fontAtlasIndex);
if (fontAtlas)
{
fontAtlas->EnsureTextureCreated();
invAtlasSize = 1.0f / fontAtlas->GetSize();
//drawCall->AsChar.Tex = fontAtlas->GetTexture();
}
else
{
invAtlasSize = 1.0f;
//drawCall->AsChar.Tex = nullptr;
}
}
// Calculate character size and atlas coordinates
const float x = pointer.X + entry.OffsetX * scale;
const float y = pointer.Y - entry.OffsetY * scale + fontHeightOffset;
@@ -1445,18 +1446,34 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
Float2 rightBottomUV = (entry.UV + entry.UVSize) * invAtlasSize;
// Add draw call
drawCall.StartIB = IBIndex;
drawCall.CountIB = 6;
DrawCalls.Add(drawCall);
drawCall->StartIB = IBIndex;
drawCall->CountIB = 6;
if (customMaterial)
{
drawCall->Type = DrawCallType::DrawCharMaterial;
drawCall->AsChar.Mat = customMaterial;
}
else
{
drawCall->Type = DrawCallType::DrawChar;
drawCall->AsChar.Mat = nullptr;
}
drawCall->AsChar.Tex = fontAtlas ? fontAtlas->GetTexture() : nullptr;
//DrawCalls[drawCallCountBefore + actualDrawCallsCount] = drawCall;
//*drawCalls = drawCall;
drawCall++;
actualDrawCallsCount++;
//DrawCalls.Add(drawCall);
WriteRect(charRect, color, upperLeftUV, rightBottomUV);
}
// Move
pointer.X += entry.AdvanceX * scale;
}
entryIndex++;
}
}
DrawCalls.Resize(drawCallCountBefore + actualDrawCallsCount);
}
void Render2D::DrawText(Font* font, const StringView& text, const TextRange& textRange, const Color& color, const TextLayoutOptions& layout, MaterialBase* customMaterial)

View File

@@ -187,11 +187,9 @@ void TextRender::UpdateLayout()
Float2 pointer = line.Location;
// Render all characters from the line
for (int32 charIndex = line.FirstCharIndex; charIndex <= line.LastCharIndex; charIndex++)
for (int32 charIndex = line.FirstCharIndex; charIndex < line.LastCharIndex; charIndex++)
{
const Char c = text[charIndex];
if (c != '\n')
{
font->GetCharacter(c, entry);
// Check if need to select/change font atlas (since characters even in the same font may be located in different atlases)
@@ -241,7 +239,7 @@ void TextRender::UpdateLayout()
const bool isWhitespace = StringUtils::IsWhitespace(c);
if (!isWhitespace && previous.IsValid)
{
kerning = entry.Font->GetKerning(previous.Character, entry.Character);
kerning = font->GetKerning(previous.Character, entry.Character);
}
else
{
@@ -308,7 +306,6 @@ void TextRender::UpdateLayout()
pointer.X += (float)entry.AdvanceX * scale;
}
}
}
drawChunk.IndicesCount = (_ib.Data.Count() / sizeof(uint16)) - drawChunk.StartIndex;
if (drawChunk.IndicesCount > 0)
{