metaforce/Runtime/GuiSys/CTextRenderBuffer.cpp

302 lines
12 KiB
C++
Raw Normal View History

2016-03-19 00:07:31 +00:00
#include "CTextRenderBuffer.hpp"
#include "CFontImageDef.hpp"
#include "Graphics/CGraphicsPalette.hpp"
2016-03-22 08:15:00 +00:00
#include "Graphics/CGraphics.hpp"
#include "CRasterFont.hpp"
#include "Graphics/CTexture.hpp"
2016-12-30 06:37:01 +00:00
#include "CTextExecuteBuffer.hpp"
#include "CFontRenderState.hpp"
#include "CInstruction.hpp"
2016-03-19 00:07:31 +00:00
namespace urde
{
2016-03-22 08:15:00 +00:00
boo::IShaderPipeline* g_TextShaderPipeline = nullptr;
boo::IVertexFormat* g_TextVtxFmt = nullptr;
boo::IShaderPipeline* g_TextImageShaderPipeline = nullptr;
boo::IVertexFormat* g_TextImageVtxFmt = nullptr;
2016-03-19 00:07:31 +00:00
CTextRenderBuffer::CTextRenderBuffer(EMode mode)
2016-03-22 08:15:00 +00:00
: x0_mode(mode) {}
2016-03-19 00:07:31 +00:00
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::BooCharacterInstance::SetMetrics(const CGlyph& glyph,
const zeus::CVector2i& offset)
2016-03-19 00:07:31 +00:00
{
2016-03-22 08:15:00 +00:00
m_pos[0].assign(offset.x, 0.f, offset.y);
m_uv[0].assign(glyph.GetStartU(), glyph.GetStartV());
2016-03-19 00:07:31 +00:00
2016-03-22 08:15:00 +00:00
m_pos[1].assign(offset.x + glyph.GetCellWidth(), 0.f, offset.y);
m_uv[1].assign(glyph.GetEndU(), glyph.GetStartV());
m_pos[2].assign(offset.x, 0.f, offset.y + glyph.GetCellHeight());
m_uv[2].assign(glyph.GetStartU(), glyph.GetEndV());
m_pos[3].assign(offset.x + glyph.GetCellWidth(), 0.f, offset.y + glyph.GetCellHeight());
m_uv[3].assign(glyph.GetEndU(), glyph.GetEndV());
2016-03-19 00:07:31 +00:00
}
2016-12-31 00:51:51 +00:00
CTextRenderBuffer::BooImage::BooImage(const CFontImageDef& imgDef, const zeus::CVector2i& offset)
: m_imageDef(imgDef)
{
zeus::CVector2f imgSize;
if (imgDef.x4_texs.size())
{
const CTexture& tex = *imgDef.x4_texs[0].GetObj();
imgSize.assign(tex.GetWidth() * imgDef.x14_pointsPerTexel.x,
tex.GetHeight() * imgDef.x14_pointsPerTexel.y);
}
m_imageData.m_pos[0].assign(offset.x, 0.f, offset.y);
m_imageData.m_uv[0].assign(0.f, 0.f);
m_imageData.m_pos[1].assign(offset.x + imgSize.x, 0.f, offset.y);
m_imageData.m_uv[1].assign(1.f, 0.f);
m_imageData.m_pos[2].assign(offset.x, 0.f, offset.y + imgSize.y);
m_imageData.m_uv[2].assign(0.f, 1.f);
m_imageData.m_pos[3].assign(offset.x + imgSize.x, 0.f, offset.y + imgSize.y);
m_imageData.m_uv[3].assign(1.f, 1.f);
}
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::BooPrimitiveMark::SetOpacity(CTextRenderBuffer& rb, float opacity)
2016-03-19 00:07:31 +00:00
{
2016-03-22 08:15:00 +00:00
switch (m_cmd)
{
case Command::CharacterRender:
{
BooFontCharacters& fc = rb.m_fontCharacters[m_bindIdx];
BooCharacterInstance& inst = fc.m_charData[m_instIdx];
inst.m_fontColor.a = opacity;
inst.m_outlineColor.a = opacity;
fc.m_dirty = true;
break;
}
case Command::ImageRender:
{
BooImage& img = rb.m_images[m_bindIdx];
img.m_imageData.m_color.a = opacity;
img.m_dirty = true;
break;
}
default: break;
}
2016-03-19 00:07:31 +00:00
}
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::CommitResources()
2016-03-19 00:07:31 +00:00
{
2016-03-22 08:15:00 +00:00
if (m_committed)
return;
m_committed = true;
2016-03-30 19:16:01 +00:00
m_booToken = CGraphics::CommitResources([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
2016-03-22 08:15:00 +00:00
{
2016-03-30 19:16:01 +00:00
m_uniBuf = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(BooUniform), 1);
2016-03-22 08:15:00 +00:00
2016-03-30 19:16:01 +00:00
for (BooFontCharacters& chs : m_fontCharacters)
2016-03-22 08:15:00 +00:00
{
2016-03-30 19:16:01 +00:00
chs.m_instBuf = ctx.newDynamicBuffer(boo::BufferUse::Vertex,
sizeof(BooCharacterInstance),
chs.m_charCount);
boo::IVertexFormat* vFmt = g_TextVtxFmt;
if (ctx.bindingNeedsVertexFormat())
2016-03-22 08:15:00 +00:00
{
2016-03-30 19:16:01 +00:00
boo::VertexElementDescriptor elems[] =
{
{chs.m_instBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 0},
{chs.m_instBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 1},
{chs.m_instBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 2},
{chs.m_instBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 3},
{chs.m_instBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 0},
{chs.m_instBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 1},
{chs.m_instBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 2},
{chs.m_instBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 3},
{chs.m_instBuf, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced, 0},
{chs.m_instBuf, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced, 1},
};
vFmt = ctx.newVertexFormat(10, elems);
}
boo::IGraphicsBuffer* uniforms[] = {m_uniBuf};
boo::ITexture* texs[] = {chs.m_font.GetObj()->GetTexture()->GetBooTexture()};
chs.m_dataBinding = ctx.newShaderDataBinding(g_TextShaderPipeline, vFmt,
nullptr, chs.m_instBuf, nullptr,
2016-04-04 06:16:03 +00:00
1, uniforms, nullptr, 1, texs);
2016-03-22 08:15:00 +00:00
}
2016-03-30 19:16:01 +00:00
for (BooImage& img : m_images)
2016-03-22 08:15:00 +00:00
{
2016-03-30 19:16:01 +00:00
img.m_instBuf = ctx.newDynamicBuffer(boo::BufferUse::Vertex, sizeof(BooImageInstance), 1);
boo::IVertexFormat* vFmt = g_TextImageVtxFmt;
if (ctx.bindingNeedsVertexFormat())
{
boo::VertexElementDescriptor elems[] =
{
{img.m_instBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 0},
{img.m_instBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 1},
{img.m_instBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 2},
{img.m_instBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 3},
{img.m_instBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 0},
{img.m_instBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 1},
{img.m_instBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 2},
{img.m_instBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 3},
{img.m_instBuf, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced, 0},
};
vFmt = ctx.newVertexFormat(9, elems);
}
boo::IGraphicsBuffer* uniforms[] = {m_uniBuf};
img.m_dataBinding.reserve(img.m_imageDef.x4_texs.size());
for (TToken<CTexture>& tex : img.m_imageDef.x4_texs)
{
boo::ITexture* texs[] = {tex->GetBooTexture()};
img.m_dataBinding.push_back(ctx.newShaderDataBinding(g_TextImageShaderPipeline, vFmt,
nullptr, img.m_instBuf, nullptr,
2016-04-04 06:16:03 +00:00
1, uniforms, nullptr, 1, texs));
2016-03-30 19:16:01 +00:00
}
2016-03-22 08:15:00 +00:00
}
2016-03-30 19:16:01 +00:00
return true;
});
2016-03-19 00:07:31 +00:00
}
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::SetMode(EMode mode)
2016-03-19 00:07:31 +00:00
{
2016-03-22 08:15:00 +00:00
if (mode == EMode::BufferFill)
{
m_images.reserve(m_imagesCount);
for (BooFontCharacters& fc : m_fontCharacters)
fc.m_charData.reserve(fc.m_charCount);
}
m_activeFontCh = -1;
2016-03-19 00:07:31 +00:00
}
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::SetPrimitiveOpacity(int idx, float opacity)
2016-03-19 00:07:31 +00:00
{
2016-03-22 08:15:00 +00:00
m_primitiveMarks[idx].SetOpacity(*this, opacity);
2016-03-19 00:07:31 +00:00
}
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::Render(const zeus::CColor& col, float time) const
2016-03-19 00:07:31 +00:00
{
2016-08-29 04:22:54 +00:00
const_cast<CTextRenderBuffer*>(this)->CommitResources();
2016-03-22 08:15:00 +00:00
2016-04-03 05:25:34 +00:00
BooUniform uniforms = {CGraphics::GetPerspectiveProjectionMatrix(true) *
2016-03-22 08:15:00 +00:00
CGraphics::g_GXModelView.toMatrix4f(), col};
m_uniBuf->load(&uniforms, sizeof(uniforms));
for (const BooFontCharacters& chs : m_fontCharacters)
{
if (chs.m_dirty)
{
chs.m_instBuf->load(chs.m_charData.data(), sizeof(BooCharacterInstance) * chs.m_charData.size());
((BooFontCharacters&)chs).m_dirty = false;
}
CGraphics::SetShaderDataBinding(chs.m_dataBinding);
CGraphics::DrawInstances(0, 4, chs.m_charData.size());
}
for (const BooImage& img : m_images)
{
if (img.m_dirty)
{
img.m_instBuf->load(&img.m_imageData, sizeof(BooImageInstance));
2016-12-31 00:51:51 +00:00
const_cast<BooImage&>(img).m_dirty = false;
2016-03-22 08:15:00 +00:00
}
int idx = int(img.m_imageDef.x0_fps * time) % img.m_dataBinding.size();
CGraphics::SetShaderDataBinding(img.m_dataBinding[idx]);
CGraphics::DrawInstances(0, 4, 1);
}
2016-03-19 00:07:31 +00:00
}
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::AddImage(const zeus::CVector2i& offset, const CFontImageDef& image)
2016-03-19 00:07:31 +00:00
{
2016-03-22 08:15:00 +00:00
if (x0_mode == EMode::AllocTally)
m_primitiveMarks.push_back({Command::ImageRender, m_imagesCount++, 0});
else
2016-12-31 00:51:51 +00:00
m_images.push_back({image, offset});
2016-03-19 00:07:31 +00:00
}
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::AddCharacter(const zeus::CVector2i& offset, wchar_t ch,
const zeus::CColor& color)
2016-03-19 00:07:31 +00:00
{
2016-03-22 08:15:00 +00:00
if (m_activeFontCh == -1)
return;
BooFontCharacters& chs = m_fontCharacters[m_activeFontCh];
if (x0_mode == EMode::AllocTally)
m_primitiveMarks.push_back({Command::CharacterRender, m_activeFontCh, chs.m_charCount++});
else
{
const CGlyph* glyph = chs.m_font.GetObj()->GetGlyph(ch);
chs.m_charData.emplace_back();
BooCharacterInstance& inst = chs.m_charData.back();
inst.SetMetrics(*glyph, offset);
inst.m_fontColor = m_main * color;
inst.m_outlineColor = m_outline * color;
}
2016-03-19 00:07:31 +00:00
}
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::AddPaletteChange(const zeus::CColor& main, const zeus::CColor& outline)
2016-03-19 00:07:31 +00:00
{
2016-03-22 08:15:00 +00:00
m_main = main;
m_outline = outline;
2016-03-19 00:07:31 +00:00
}
2016-03-22 08:15:00 +00:00
void CTextRenderBuffer::AddFontChange(const TToken<CRasterFont>& font)
2016-03-19 00:07:31 +00:00
{
2016-03-22 08:15:00 +00:00
for (int i=0 ; i<m_fontCharacters.size() ; ++i)
{
BooFontCharacters& chs = m_fontCharacters[i];
if (*chs.m_font.GetObjectTag() == *font.GetObjectTag())
{
m_activeFontCh = i;
return;
}
}
m_activeFontCh = m_fontCharacters.size();
m_fontCharacters.push_back({font});
2016-03-19 00:07:31 +00:00
}
2016-12-31 00:51:51 +00:00
bool CTextRenderBuffer::HasSpaceAvailable(const zeus::CVector2i& origin,
const zeus::CVector2i& extent) const
2016-12-30 06:37:01 +00:00
{
2016-12-31 00:51:51 +00:00
std::pair<zeus::CVector2i, zeus::CVector2i> bounds = AccumulateTextBounds();
if (bounds.first.x > bounds.second.x)
return true;
2016-12-30 06:37:01 +00:00
2016-12-31 00:51:51 +00:00
if (0 < origin.y)
return false;
2016-12-30 06:37:01 +00:00
2016-12-31 00:51:51 +00:00
zeus::CVector2i size = bounds.second - bounds.first;
return size.y <= extent.y;
}
2016-12-30 06:37:01 +00:00
2016-12-31 00:51:51 +00:00
std::pair<zeus::CVector2i, zeus::CVector2i> CTextRenderBuffer::AccumulateTextBounds() const
{
std::pair<zeus::CVector2i, zeus::CVector2i> ret = std::make_pair(zeus::CVector2i{INT_MAX, INT_MAX},
zeus::CVector2i{INT_MIN, INT_MIN});
2016-12-30 06:37:01 +00:00
2016-12-31 00:51:51 +00:00
for (const BooFontCharacters& chars : m_fontCharacters)
{
for (const BooCharacterInstance& charInst : chars.m_charData)
{
ret.first.x = std::min(ret.first.x, int(charInst.m_pos[0].x));
ret.first.y = std::min(ret.first.y, int(charInst.m_pos[0].z));
ret.second.x = std::max(ret.second.x, int(charInst.m_pos[3].x));
ret.second.y = std::max(ret.second.y, int(charInst.m_pos[3].z));
2016-12-30 06:37:01 +00:00
}
}
2016-12-31 00:51:51 +00:00
for (const BooImage& imgs : m_images)
{
ret.first.x = std::min(ret.first.x, int(imgs.m_imageData.m_pos[0].x));
ret.first.y = std::min(ret.first.y, int(imgs.m_imageData.m_pos[0].z));
ret.second.x = std::max(ret.second.x, int(imgs.m_imageData.m_pos[3].x));
ret.second.y = std::max(ret.second.y, int(imgs.m_imageData.m_pos[3].z));
}
return ret;
2016-12-30 06:37:01 +00:00
}
2016-03-19 00:07:31 +00:00
}