_font drawcall opt
This commit is contained in:
@@ -233,9 +233,11 @@ void Font::ProcessText(const StringView& text, Array<FontLineCache>& outputLines
|
||||
if (lastMoveLine)
|
||||
break;
|
||||
}
|
||||
|
||||
characterEntries.Add(entry);
|
||||
characterKernings.Add((float)kerning * scale);
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -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,27 +1380,42 @@ 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')
|
||||
//font->GetCharacter(c, entry);
|
||||
const FontCharacterEntry& entry = characterEntries[entryIndex];
|
||||
float kerning = characterKernings[entryIndex];
|
||||
|
||||
// Get kerning
|
||||
const bool isWhitespace = StringUtils::IsWhitespace(c); // kerning == inf;
|
||||
/*if (!isWhitespace && previous.IsValid)
|
||||
{
|
||||
// Get character entry
|
||||
//font->GetCharacter(currentChar, entry, enableFallbackFonts);
|
||||
const FontCharacterEntry& entry = characterEntries[entryIndex];
|
||||
float kerning = characterKernings[entryIndex];
|
||||
kerning = font->GetKerning(previous.Character, entry.Character);
|
||||
}
|
||||
else
|
||||
{
|
||||
kerning = 0;
|
||||
}
|
||||
pointer.X += (float)kerning * scale;*/
|
||||
|
||||
pointer.X += kerning;
|
||||
previous = entry;
|
||||
|
||||
// Omit whitespace characters
|
||||
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)
|
||||
{
|
||||
@@ -1406,57 +1426,54 @@ void Render2D::DrawText(Font* font, const StringView& text, const Color& color,
|
||||
{
|
||||
fontAtlas->EnsureTextureCreated();
|
||||
invAtlasSize = 1.0f / fontAtlas->GetSize();
|
||||
drawCall.AsChar.Tex = fontAtlas->GetTexture();
|
||||
//drawCall->AsChar.Tex = fontAtlas->GetTexture();
|
||||
}
|
||||
else
|
||||
{
|
||||
invAtlasSize = 1.0f;
|
||||
drawCall.AsChar.Tex = nullptr;
|
||||
//drawCall->AsChar.Tex = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Get kerning
|
||||
const bool isWhitespace = StringUtils::IsWhitespace(currentChar);
|
||||
/*if (!isWhitespace && previous.IsValid)
|
||||
// Calculate character size and atlas coordinates
|
||||
const float x = pointer.X + entry.OffsetX * scale;
|
||||
const float y = pointer.Y - entry.OffsetY * scale + fontHeightOffset;
|
||||
|
||||
Rectangle charRect(x, y, entry.UVSize.X * scale, entry.UVSize.Y * scale);
|
||||
charRect.Offset(layout.Bounds.Location);
|
||||
|
||||
Float2 upperLeftUV = entry.UV * invAtlasSize;
|
||||
Float2 rightBottomUV = (entry.UV + entry.UVSize) * invAtlasSize;
|
||||
|
||||
// Add draw call
|
||||
drawCall->StartIB = IBIndex;
|
||||
drawCall->CountIB = 6;
|
||||
if (customMaterial)
|
||||
{
|
||||
kerning = entry.Font->GetKerning(previous.Character, entry.Character);
|
||||
drawCall->Type = DrawCallType::DrawCharMaterial;
|
||||
drawCall->AsChar.Mat = customMaterial;
|
||||
}
|
||||
else
|
||||
{
|
||||
kerning = 0;
|
||||
drawCall->Type = DrawCallType::DrawChar;
|
||||
drawCall->AsChar.Mat = nullptr;
|
||||
}
|
||||
pointer.X += (float)kerning * scale;*/
|
||||
|
||||
pointer.X += kerning;
|
||||
previous = entry;
|
||||
|
||||
// Omit whitespace characters
|
||||
if (!isWhitespace)
|
||||
//if (xAdvance != 0.0f)
|
||||
{
|
||||
// Calculate character size and atlas coordinates
|
||||
const float x = pointer.X + entry.OffsetX * scale;
|
||||
const float y = pointer.Y - entry.OffsetY * scale + fontHeightOffset;
|
||||
|
||||
Rectangle charRect(x, y, entry.UVSize.X * scale, entry.UVSize.Y * scale);
|
||||
charRect.Offset(layout.Bounds.Location);
|
||||
|
||||
Float2 upperLeftUV = entry.UV * invAtlasSize;
|
||||
Float2 rightBottomUV = (entry.UV + entry.UVSize) * invAtlasSize;
|
||||
|
||||
// Add draw call
|
||||
drawCall.StartIB = IBIndex;
|
||||
drawCall.CountIB = 6;
|
||||
DrawCalls.Add(drawCall);
|
||||
WriteRect(charRect, color, upperLeftUV, rightBottomUV);
|
||||
}
|
||||
|
||||
// Move
|
||||
pointer.X += entry.AdvanceX * scale;
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user