_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

@@ -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;