1
0
forked from mirrors/0ad

Makes TextRenderer allocates using LinearAllocator

Also replaces DynamicArena for model and terrain rendering by
LinearAllocator.
This commit is contained in:
Vladislav Belov
2025-10-12 00:53:12 +02:00
parent e4de94415e
commit 893f192eaa
7 changed files with 102 additions and 70 deletions
+9 -11
View File
@@ -52,6 +52,7 @@ constexpr size_t MAX_CHAR_COUNT_PER_BATCH = 65536 / 4;
} // anonymous namespace
CTextRenderer::CTextRenderer()
: m_ScopedLinearAllocator{g_Renderer.GetLinearAllocator()}, m_Batches(m_ScopedLinearAllocator)
{
ResetTranslate();
SetCurrentColor(CColor(1.0f, 1.0f, 1.0f, 1.0f));
@@ -174,7 +175,7 @@ void CTextRenderer::PutString(float x, float y, const std::wstring* buf, bool ow
// If any state has changed since the last batch, start a new batch
if (m_Dirty)
{
SBatch batch;
SBatch batch{m_ScopedLinearAllocator};
batch.chars = 0;
batch.translate = m_Translate;
batch.color = m_Color;
@@ -210,16 +211,16 @@ void CTextRenderer::Render(
const CVector2D& transformScale, const CVector2D& translation,
const bool debugFontBox, const CColor& debugBoxColor)
{
std::vector<u16> indices;
std::vector<CVector2D> positions;
std::vector<CVector2D> uvs;
std::vector<u16, ProxyAllocator<u16, PS::Memory::ScopedLinearAllocator>> indices{m_ScopedLinearAllocator};
std::vector<CVector2D, ProxyAllocator<CVector2D, PS::Memory::ScopedLinearAllocator>> positions{m_ScopedLinearAllocator};
std::vector<CVector2D, ProxyAllocator<CVector2D, PS::Memory::ScopedLinearAllocator>> uvs{m_ScopedLinearAllocator};
// Try to merge non-consecutive batches that share the same font/color/translate:
// sort the batch list by font, then merge the runs of adjacent compatible batches
m_Batches.sort(SBatchCompare());
for (std::list<SBatch>::iterator it = m_Batches.begin(); it != m_Batches.end(); )
for (SBatchList::iterator it = m_Batches.begin(); it != m_Batches.end(); )
{
std::list<SBatch>::iterator next = std::next(it);
SBatchList::iterator next = std::next(it);
if (next != m_Batches.end() && it->chars + next->chars <= MAX_CHAR_COUNT_PER_BATCH && it->font == next->font && it->color == next->color && it->translate == next->translate)
{
it->chars += next->chars;
@@ -238,10 +239,8 @@ void CTextRenderer::Render(
bool translationChanged = false;
CTexture* lastTexture = nullptr;
for (std::list<SBatch>::iterator it = m_Batches.begin(); it != m_Batches.end(); ++it)
for (SBatch& batch : m_Batches)
{
SBatch& batch = *it;
if (lastTexture != batch.font->GetTexture().get())
{
batch.font->InitalizeAtlasTextureIfNeeded(deviceCommandContext);
@@ -294,9 +293,8 @@ void CTextRenderer::Render(
idx = 0;
};
for (std::list<SBatchRun>::iterator runit = batch.runs.begin(); runit != batch.runs.end(); ++runit)
for (SBatchRun& run : batch.runs)
{
SBatchRun& run = *runit;
float x{std::ceil(run.x)};
float y{std::ceil(run.y)};
for (size_t i = 0; i < run.text->size(); ++i)
+10 -2
View File
@@ -19,9 +19,11 @@
#define INCLUDED_TEXTRENDERER
#include "graphics/Color.h"
#include "lib/allocators/STLAllocators.h"
#include "maths/Rect.h"
#include "maths/Vector2D.h"
#include "ps/CStrIntern.h"
#include "ps/memory/LinearAllocator.h"
#include <cstddef>
#include <list>
@@ -159,11 +161,16 @@ private:
CVector2D translate;
CColor color;
CFont* font;
std::list<SBatchRun> runs;
using SBatchRunList = std::list<SBatchRun, ProxyAllocator<SBatchRun, PS::Memory::ScopedLinearAllocator>>;
SBatchRunList runs;
SBatch(PS::Memory::ScopedLinearAllocator& allocator) : runs{allocator} {}
};
void PutString(float x, float y, const std::wstring* buf, bool owned);
PS::Memory::ScopedLinearAllocator m_ScopedLinearAllocator;
CVector2D m_Translate;
CRect m_Clipping;
@@ -173,7 +180,8 @@ private:
bool m_Dirty = true;
std::list<SBatch> m_Batches;
using SBatchList = std::list<SBatch, ProxyAllocator<SBatch, PS::Memory::ScopedLinearAllocator>>;
SBatchList m_Batches;
};
#endif // INCLUDED_TEXTRENDERER