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

View File

@@ -1363,9 +1363,14 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
characterKernings.Clear(); characterKernings.Clear();
font->ProcessText(text, Lines, layout, characterEntries, characterKernings); 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 // Render all lines
//FontCharacterEntry entry; //FontCharacterEntry entry;
Render2DDrawCall drawCall; /*Render2DDrawCall drawCall;
if (customMaterial) if (customMaterial)
{ {
drawCall.Type = DrawCallType::DrawCharMaterial; drawCall.Type = DrawCallType::DrawCharMaterial;
@@ -1375,51 +1380,28 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
{ {
drawCall.Type = DrawCallType::DrawChar; drawCall.Type = DrawCallType::DrawChar;
drawCall.AsChar.Mat = nullptr; drawCall.AsChar.Mat = nullptr;
} }*/
int32 entryIndex = 0; int32 entryIndex = 0;
int32 actualDrawCallsCount = 0;
for (int32 lineIndex = 0; lineIndex < Lines.Count(); lineIndex++) for (int32 lineIndex = 0; lineIndex < Lines.Count(); lineIndex++)
{ {
const FontLineCache& line = Lines[lineIndex]; const FontLineCache& line = Lines[lineIndex];
Float2 pointer = line.Location; Float2 pointer = line.Location;
// Render all characters from the line // 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 c = text[charIndex];
const Char currentChar = text[charIndex];
// Check if it isn't a newline character //font->GetCharacter(c, entry);
if (currentChar != '\n')
{
// Get character entry
//font->GetCharacter(currentChar, entry, enableFallbackFonts);
const FontCharacterEntry& entry = characterEntries[entryIndex]; const FontCharacterEntry& entry = characterEntries[entryIndex];
float kerning = characterKernings[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 // Get kerning
const bool isWhitespace = StringUtils::IsWhitespace(currentChar); const bool isWhitespace = StringUtils::IsWhitespace(c); // kerning == inf;
/*if (!isWhitespace && previous.IsValid) /*if (!isWhitespace && previous.IsValid)
{ {
kerning = entry.Font->GetKerning(previous.Character, entry.Character); kerning = font->GetKerning(previous.Character, entry.Character);
} }
else else
{ {
@@ -1434,6 +1416,25 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
if (!isWhitespace) if (!isWhitespace)
//if (xAdvance != 0.0f) //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 // Calculate character size and atlas coordinates
const float x = pointer.X + entry.OffsetX * scale; const float x = pointer.X + entry.OffsetX * scale;
const float y = pointer.Y - entry.OffsetY * scale + fontHeightOffset; 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; Float2 rightBottomUV = (entry.UV + entry.UVSize) * invAtlasSize;
// Add draw call // Add draw call
drawCall.StartIB = IBIndex; drawCall->StartIB = IBIndex;
drawCall.CountIB = 6; drawCall->CountIB = 6;
DrawCalls.Add(drawCall); 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); WriteRect(charRect, color, upperLeftUV, rightBottomUV);
} }
// Move // Move
pointer.X += entry.AdvanceX * scale; pointer.X += entry.AdvanceX * scale;
}
entryIndex++; entryIndex++;
} }
} }
DrawCalls.Resize(drawCallCountBefore + actualDrawCallsCount);
} }
void Render2D::DrawText(Font* font, const StringView& text, const TextRange& textRange, const Color& color, const TextLayoutOptions& layout, MaterialBase* customMaterial) 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; Float2 pointer = line.Location;
// Render all characters from the line // 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]; const Char c = text[charIndex];
if (c != '\n')
{
font->GetCharacter(c, entry); 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) // 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); const bool isWhitespace = StringUtils::IsWhitespace(c);
if (!isWhitespace && previous.IsValid) if (!isWhitespace && previous.IsValid)
{ {
kerning = entry.Font->GetKerning(previous.Character, entry.Character); kerning = font->GetKerning(previous.Character, entry.Character);
} }
else else
{ {
@@ -308,7 +306,6 @@ void TextRender::UpdateLayout()
pointer.X += (float)entry.AdvanceX * scale; pointer.X += (float)entry.AdvanceX * scale;
} }
} }
}
drawChunk.IndicesCount = (_ib.Data.Count() / sizeof(uint16)) - drawChunk.StartIndex; drawChunk.IndicesCount = (_ib.Data.Count() / sizeof(uint16)) - drawChunk.StartIndex;
if (drawChunk.IndicesCount > 0) if (drawChunk.IndicesCount > 0)
{ {