_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)
|
||||
|
||||
@@ -187,126 +187,123 @@ 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)
|
||||
if (fontAtlas == nullptr || entry.TextureIndex != drawChunk.FontAtlasIndex)
|
||||
{
|
||||
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)
|
||||
if (fontAtlas == nullptr || entry.TextureIndex != drawChunk.FontAtlasIndex)
|
||||
// Check if need to change atlas (enqueue draw chunk)
|
||||
if (fontAtlas)
|
||||
{
|
||||
// Check if need to change atlas (enqueue draw chunk)
|
||||
if (fontAtlas)
|
||||
drawChunk.IndicesCount = (_ib.Data.Count() / sizeof(uint16)) - drawChunk.StartIndex;
|
||||
if (drawChunk.IndicesCount > 0)
|
||||
{
|
||||
drawChunk.IndicesCount = (_ib.Data.Count() / sizeof(uint16)) - drawChunk.StartIndex;
|
||||
if (drawChunk.IndicesCount > 0)
|
||||
{
|
||||
_drawChunks.Add(drawChunk);
|
||||
}
|
||||
drawChunk.StartIndex = indexCounter;
|
||||
}
|
||||
|
||||
// Get texture atlas that contains current character
|
||||
drawChunk.FontAtlasIndex = entry.TextureIndex;
|
||||
fontAtlas = FontManager::GetAtlas(drawChunk.FontAtlasIndex);
|
||||
if (fontAtlas)
|
||||
{
|
||||
fontAtlas->EnsureTextureCreated();
|
||||
invAtlasSize = 1.0f / fontAtlas->GetSize();
|
||||
}
|
||||
else
|
||||
{
|
||||
invAtlasSize = 1.0f;
|
||||
}
|
||||
|
||||
// Setup material
|
||||
drawChunk.Material = Content::CreateVirtualAsset<MaterialInstance>();
|
||||
drawChunk.Material->SetBaseMaterial(Material.Get());
|
||||
for (auto& param : drawChunk.Material->Params)
|
||||
param.SetIsOverride(false);
|
||||
|
||||
// Set the font parameter
|
||||
static StringView FontParamName = TEXT("Font");
|
||||
const auto param = drawChunk.Material->Params.Get(FontParamName);
|
||||
if (param && param->GetParameterType() == MaterialParameterType::Texture)
|
||||
{
|
||||
param->SetValue(Variant(fontAtlas));
|
||||
param->SetIsOverride(true);
|
||||
_drawChunks.Add(drawChunk);
|
||||
}
|
||||
drawChunk.StartIndex = indexCounter;
|
||||
}
|
||||
|
||||
// Get kerning
|
||||
const bool isWhitespace = StringUtils::IsWhitespace(c);
|
||||
if (!isWhitespace && previous.IsValid)
|
||||
// Get texture atlas that contains current character
|
||||
drawChunk.FontAtlasIndex = entry.TextureIndex;
|
||||
fontAtlas = FontManager::GetAtlas(drawChunk.FontAtlasIndex);
|
||||
if (fontAtlas)
|
||||
{
|
||||
kerning = entry.Font->GetKerning(previous.Character, entry.Character);
|
||||
fontAtlas->EnsureTextureCreated();
|
||||
invAtlasSize = 1.0f / fontAtlas->GetSize();
|
||||
}
|
||||
else
|
||||
{
|
||||
kerning = 0;
|
||||
invAtlasSize = 1.0f;
|
||||
}
|
||||
pointer.X += (float)kerning * scale;
|
||||
previous = entry;
|
||||
|
||||
// Omit whitespace characters
|
||||
if (!isWhitespace)
|
||||
// Setup material
|
||||
drawChunk.Material = Content::CreateVirtualAsset<MaterialInstance>();
|
||||
drawChunk.Material->SetBaseMaterial(Material.Get());
|
||||
for (auto& param : drawChunk.Material->Params)
|
||||
param.SetIsOverride(false);
|
||||
|
||||
// Set the font parameter
|
||||
static StringView FontParamName = TEXT("Font");
|
||||
const auto param = drawChunk.Material->Params.Get(FontParamName);
|
||||
if (param && param->GetParameterType() == MaterialParameterType::Texture)
|
||||
{
|
||||
// Calculate character size and atlas coordinates
|
||||
const float x = pointer.X + (float)entry.OffsetX * scale;
|
||||
const float y = pointer.Y + (float)(font->GetHeight() + font->GetDescender() - entry.OffsetY) * scale;
|
||||
param->SetValue(Variant(fontAtlas));
|
||||
param->SetIsOverride(true);
|
||||
}
|
||||
}
|
||||
|
||||
Rectangle charRect(x, y, entry.UVSize.X * scale, entry.UVSize.Y * scale);
|
||||
charRect.Offset(_layoutOptions.Bounds.Location);
|
||||
// Get kerning
|
||||
const bool isWhitespace = StringUtils::IsWhitespace(c);
|
||||
if (!isWhitespace && previous.IsValid)
|
||||
{
|
||||
kerning = font->GetKerning(previous.Character, entry.Character);
|
||||
}
|
||||
else
|
||||
{
|
||||
kerning = 0;
|
||||
}
|
||||
pointer.X += (float)kerning * scale;
|
||||
previous = entry;
|
||||
|
||||
Float2 upperLeftUV = entry.UV * invAtlasSize;
|
||||
Float2 rightBottomUV = (entry.UV + entry.UVSize) * invAtlasSize;
|
||||
// Omit whitespace characters
|
||||
if (!isWhitespace)
|
||||
{
|
||||
// Calculate character size and atlas coordinates
|
||||
const float x = pointer.X + (float)entry.OffsetX * scale;
|
||||
const float y = pointer.Y + (float)(font->GetHeight() + font->GetDescender() - entry.OffsetY) * scale;
|
||||
|
||||
// Calculate bitangent sign
|
||||
Float3 normal = Float3::UnitZ;
|
||||
Float3 tangent = Float3::UnitX;
|
||||
byte sign = 0;
|
||||
Rectangle charRect(x, y, entry.UVSize.X * scale, entry.UVSize.Y * scale);
|
||||
charRect.Offset(_layoutOptions.Bounds.Location);
|
||||
|
||||
// Write vertices
|
||||
VB0ElementType vb0;
|
||||
VB1ElementType vb1;
|
||||
VB2ElementType vb2;
|
||||
Float2 upperLeftUV = entry.UV * invAtlasSize;
|
||||
Float2 rightBottomUV = (entry.UV + entry.UVSize) * invAtlasSize;
|
||||
|
||||
// Calculate bitangent sign
|
||||
Float3 normal = Float3::UnitZ;
|
||||
Float3 tangent = Float3::UnitX;
|
||||
byte sign = 0;
|
||||
|
||||
// Write vertices
|
||||
VB0ElementType vb0;
|
||||
VB1ElementType vb1;
|
||||
VB2ElementType vb2;
|
||||
#define WRITE_VB(pos, uv) \
|
||||
vb0.Position = Float3(-pos, 0.0f); \
|
||||
box.Merge(vb0.Position); \
|
||||
_vb0.Write(vb0); \
|
||||
vb1.TexCoord = Half2(uv); \
|
||||
vb1.Normal = Float1010102(normal * 0.5f + 0.5f, 0); \
|
||||
vb1.Tangent = Float1010102(tangent * 0.5f + 0.5f, sign); \
|
||||
vb1.LightmapUVs = Half2::Zero; \
|
||||
_vb1.Write(vb1); \
|
||||
vb2.Color = color; \
|
||||
_vb2.Write(vb2)
|
||||
//
|
||||
WRITE_VB(charRect.GetBottomRight(), rightBottomUV);
|
||||
WRITE_VB(charRect.GetBottomLeft(), Float2(upperLeftUV.X, rightBottomUV.Y));
|
||||
WRITE_VB(charRect.GetUpperLeft(), upperLeftUV);
|
||||
WRITE_VB(charRect.GetUpperRight(), Float2(rightBottomUV.X, upperLeftUV.Y));
|
||||
//
|
||||
vb0.Position = Float3(-pos, 0.0f); \
|
||||
box.Merge(vb0.Position); \
|
||||
_vb0.Write(vb0); \
|
||||
vb1.TexCoord = Half2(uv); \
|
||||
vb1.Normal = Float1010102(normal * 0.5f + 0.5f, 0); \
|
||||
vb1.Tangent = Float1010102(tangent * 0.5f + 0.5f, sign); \
|
||||
vb1.LightmapUVs = Half2::Zero; \
|
||||
_vb1.Write(vb1); \
|
||||
vb2.Color = color; \
|
||||
_vb2.Write(vb2)
|
||||
//
|
||||
WRITE_VB(charRect.GetBottomRight(), rightBottomUV);
|
||||
WRITE_VB(charRect.GetBottomLeft(), Float2(upperLeftUV.X, rightBottomUV.Y));
|
||||
WRITE_VB(charRect.GetUpperLeft(), upperLeftUV);
|
||||
WRITE_VB(charRect.GetUpperRight(), Float2(rightBottomUV.X, upperLeftUV.Y));
|
||||
//
|
||||
#undef WRITE_VB
|
||||
|
||||
const uint16 startVertex = vertexCounter;
|
||||
vertexCounter += 4;
|
||||
indexCounter += 6;
|
||||
const uint16 startVertex = vertexCounter;
|
||||
vertexCounter += 4;
|
||||
indexCounter += 6;
|
||||
|
||||
// Write indices
|
||||
_ib.Write((uint16)(startVertex + (uint16)0));
|
||||
_ib.Write((uint16)(startVertex + (uint16)1));
|
||||
_ib.Write((uint16)(startVertex + (uint16)2));
|
||||
_ib.Write((uint16)(startVertex + (uint16)2));
|
||||
_ib.Write((uint16)(startVertex + (uint16)3));
|
||||
_ib.Write((uint16)(startVertex + (uint16)0));
|
||||
}
|
||||
|
||||
// Move
|
||||
pointer.X += (float)entry.AdvanceX * scale;
|
||||
// Write indices
|
||||
_ib.Write((uint16)(startVertex + (uint16)0));
|
||||
_ib.Write((uint16)(startVertex + (uint16)1));
|
||||
_ib.Write((uint16)(startVertex + (uint16)2));
|
||||
_ib.Write((uint16)(startVertex + (uint16)2));
|
||||
_ib.Write((uint16)(startVertex + (uint16)3));
|
||||
_ib.Write((uint16)(startVertex + (uint16)0));
|
||||
}
|
||||
|
||||
// Move
|
||||
pointer.X += (float)entry.AdvanceX * scale;
|
||||
}
|
||||
}
|
||||
drawChunk.IndicesCount = (_ib.Data.Count() / sizeof(uint16)) - drawChunk.StartIndex;
|
||||
|
||||
Reference in New Issue
Block a user