metaforce/specter/lib/TextView.cpp

364 lines
12 KiB
C++
Raw Normal View History

2015-11-21 23:45:02 +00:00
#include "Specter/TextView.hpp"
2015-11-26 00:24:01 +00:00
#include "Specter/ViewSystem.hpp"
2015-11-25 01:46:30 +00:00
#include "utf8proc.h"
2015-11-21 23:45:02 +00:00
2015-11-26 07:35:43 +00:00
#include <freetype/internal/internal.h>
#include <freetype/internal/ftobjs.h>
2015-11-21 23:45:02 +00:00
namespace Specter
{
2015-11-25 01:46:30 +00:00
static LogVisor::LogModule Log("Specter::TextView");
2015-11-26 00:24:01 +00:00
void TextView::System::init(boo::GLDataFactory* factory, FontCache* fcache)
2015-11-25 01:46:30 +00:00
{
2015-11-26 00:24:01 +00:00
m_fcache = fcache;
2015-11-25 01:46:30 +00:00
static const char* VS =
"#version 330\n"
2015-11-26 00:24:01 +00:00
"layout(location=0) in vec3 posIn[4];\n"
"layout(location=4) in mat4 mvMtx;\n"
"layout(location=8) in vec3 uvIn[4];\n"
"layout(location=12) in vec4 colorIn;\n"
SPECTER_VIEW_VERT_BLOCK_GLSL
2015-11-25 01:46:30 +00:00
"struct VertToFrag\n"
"{\n"
" vec3 uv;\n"
" vec4 color;\n"
"};\n"
"out VertToFrag vtf;\n"
"void main()\n"
"{\n"
" vtf.uv = uvIn[gl_VertexID];\n"
" vtf.color = colorIn;\n"
2015-11-26 00:24:01 +00:00
" gl_Position = mv * mvMtx * vec4(posIn[gl_VertexID], 1.0);\n"
2015-11-25 01:46:30 +00:00
"}\n";
static const char* FSReg =
"#version 330\n"
"uniform sampler2DArray fontTex;\n"
"struct VertToFrag\n"
"{\n"
" vec3 uv;\n"
" vec4 color;\n"
"};\n"
"in VertToFrag vtf;\n"
"layout(location=0) out vec4 colorOut;\n"
"void main()\n"
"{\n"
" colorOut = vtf.color;\n"
" colorOut.a = texture(fontTex, vtf.uv).r;\n"
"}\n";
static const char* FSSubpixel =
"#version 330\n"
"uniform sampler2DArray fontTex;\n"
"struct VertToFrag\n"
"{\n"
" vec3 uv;\n"
" vec4 color;\n"
"};\n"
"in VertToFrag vtf;\n"
"layout(location=0, index=0) out vec4 colorOut;\n"
"layout(location=0, index=1) out vec4 blendOut;\n"
"void main()\n"
"{\n"
" colorOut = vtf.color;\n"
" blendOut = texture(fontTex, vtf.uv);\n"
"}\n";
2015-11-26 00:24:01 +00:00
static const char* BlockNames[] = {"SpecterViewBlock"};
2015-11-25 01:46:30 +00:00
2015-11-26 00:24:01 +00:00
m_regular =
factory->newShaderPipeline(VS, FSReg, 1, "fontTex", 1, BlockNames,
2015-11-25 01:46:30 +00:00
boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha,
2015-11-26 23:03:56 +00:00
false, false, false);
2015-11-25 01:46:30 +00:00
2015-11-26 00:24:01 +00:00
m_subpixel =
factory->newShaderPipeline(VS, FSSubpixel, 1, "fontTex", 1, BlockNames,
2015-11-25 01:46:30 +00:00
boo::BlendFactor::SrcColor1, boo::BlendFactor::InvSrcColor1,
2015-11-26 23:03:56 +00:00
false, false, false);
2015-11-25 01:46:30 +00:00
}
2015-11-27 22:20:22 +00:00
void TextView::System::init(boo::ID3DDataFactory* factory, FontCache* fcache)
{
m_fcache = fcache;
static const char* VS =
"struct VertData\n"
"{\n"
" float3 posIn[4] : POSITION;\n"
" float4x4 mvMtx : MODELVIEW;\n"
" float3 uvIn[4] : UV;\n"
" float4 colorIn : COLOR;\n"
"};\n"
SPECTER_VIEW_VERT_BLOCK_HLSL
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float3 uv : UV;\n"
" float4 color : COLOR;\n"
"};\n"
"VertToFrag main(in VertData v, in uint vertId : SV_VertexID)\n"
"{\n"
" VertToFrag vtf;\n"
" vtf.uv = v.uvIn[vertId];\n"
" vtf.color = v.colorIn;\n"
" vtf.position = mul(mv, mul(v.mvMtx, float4(v.posIn[vertId], 1.0)));\n"
" return vtf;\n"
"}\n";
static const char* FSReg =
"Texture2DArray fontTex : register(t0);\n"
"SamplerState samp : register(s0);\n"
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float3 uv : UV;\n"
" float4 color : COLOR;\n"
"};\n"
"float4 main(in VertToFrag vtf) : SV_Target0\n"
"{\n"
" float4 colorOut = vtf.color;\n"
" colorOut.a = fontTex.Sample(samp, vtf.uv).r;\n"
" return colorOut;\n"
"}\n";
static const char* FSSubpixel =
"Texture2DArray fontTex : register(t0);\n"
"SamplerState samp : register(s0);\n"
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float3 uv : UV;\n"
" float4 color : COLOR;\n"
"};\n"
"struct BlendOut\n"
"{\n"
" float4 colorOut : SV_Target0;\n"
" float4 blendOut : SV_Target1;\n"
"};\n"
"BlendOut main(in VertToFrag vtf)\n"
"{\n"
" BlendOut ret;\n"
" ret.colorOut = vtf.color;\n"
" ret.blendOut = fontTex.Sample(samp, vtf.uv);\n"
" return ret;\n"
"}\n";
boo::VertexElementDescriptor vdescs[] =
{
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 0},
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 1},
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 2},
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 3},
{nullptr, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 0},
{nullptr, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 1},
{nullptr, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 2},
{nullptr, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 3},
{nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 0},
{nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 1},
{nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 2},
{nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 3},
{nullptr, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced}
};
m_vtxFmt = factory->newVertexFormat(13, vdescs);
ComPtr<ID3DBlob> blobVert;
ComPtr<ID3DBlob> blobFrag;
ComPtr<ID3DBlob> blobPipe;
m_regular =
factory->newShaderPipeline(VS, FSReg, blobVert, blobFrag, blobPipe, m_vtxFmt,
boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha,
false, false, false);
blobVert.Reset();
blobFrag.Reset();
blobPipe.Reset();
m_subpixel =
factory->newShaderPipeline(VS, FSSubpixel, blobVert, blobFrag, blobPipe, m_vtxFmt,
boo::BlendFactor::SrcColor1, boo::BlendFactor::InvSrcColor1,
false, false, false);
}
2015-11-26 07:35:43 +00:00
TextView::TextView(ViewSystem& system, FontTag font, size_t capacity)
2015-11-26 00:24:01 +00:00
: View(system),
2015-11-26 07:35:43 +00:00
m_capacity(capacity),
2015-11-26 00:24:01 +00:00
m_fontAtlas(system.m_textSystem.m_fcache->lookupAtlas(font))
2015-11-25 01:46:30 +00:00
{
m_glyphBuf =
system.m_factory->newDynamicBuffer(boo::BufferUse::Vertex,
2015-11-26 07:35:43 +00:00
sizeof(RenderGlyph), capacity);
2015-11-25 01:46:30 +00:00
2015-11-26 00:24:01 +00:00
if (!system.m_textSystem.m_vtxFmt)
2015-11-25 01:46:30 +00:00
{
boo::VertexElementDescriptor vdescs[] =
{
2015-11-26 00:24:01 +00:00
{m_glyphBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 0},
{m_glyphBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 1},
{m_glyphBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 2},
{m_glyphBuf, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 3},
2015-11-25 01:46:30 +00:00
{m_glyphBuf, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 0},
{m_glyphBuf, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 1},
{m_glyphBuf, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 2},
{m_glyphBuf, nullptr, boo::VertexSemantic::ModelView | boo::VertexSemantic::Instanced, 3},
{m_glyphBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 0},
{m_glyphBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 1},
{m_glyphBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 2},
{m_glyphBuf, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 3},
{m_glyphBuf, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced}
};
2015-11-26 07:35:43 +00:00
m_vtxFmt = system.m_factory->newVertexFormat(13, vdescs);
boo::ITexture* texs[] = {m_fontAtlas.texture()};
m_shaderBinding = system.m_factory->newShaderDataBinding(system.m_textSystem.m_regular, m_vtxFmt,
nullptr, m_glyphBuf, nullptr, 1,
(boo::IGraphicsBuffer**)&m_viewVertBlockBuf,
2015-11-27 22:20:22 +00:00
1, texs);
}
else
{
boo::ITexture* texs[] = {m_fontAtlas.texture()};
m_shaderBinding = system.m_factory->newShaderDataBinding(system.m_textSystem.m_regular, system.m_textSystem.m_vtxFmt,
nullptr, m_glyphBuf, nullptr, 1,
(boo::IGraphicsBuffer**)&m_viewVertBlockBuf,
2015-11-26 07:35:43 +00:00
1, texs);
2015-11-25 01:46:30 +00:00
}
2015-11-26 07:35:43 +00:00
m_glyphs.reserve(capacity);
2015-11-25 01:46:30 +00:00
}
2015-11-26 07:35:43 +00:00
TextView::RenderGlyph::RenderGlyph(int& adv, const FontAtlas::Glyph& glyph, const Zeus::CColor& defaultColor)
2015-11-26 00:24:01 +00:00
{
m_pos[0].assign(adv + glyph.m_leftPadding, glyph.m_verticalOffset + glyph.m_height, 0.f);
m_pos[1].assign(adv + glyph.m_leftPadding, glyph.m_verticalOffset, 0.f);
m_pos[2].assign(adv + glyph.m_leftPadding + glyph.m_width, glyph.m_verticalOffset + glyph.m_height, 0.f);
m_pos[3].assign(adv + glyph.m_leftPadding + glyph.m_width, glyph.m_verticalOffset, 0.f);
m_uv[0].assign(glyph.m_uv[0], glyph.m_uv[1], glyph.m_layerFloat);
m_uv[1].assign(glyph.m_uv[0], glyph.m_uv[3], glyph.m_layerFloat);
m_uv[2].assign(glyph.m_uv[2], glyph.m_uv[1], glyph.m_layerFloat);
m_uv[3].assign(glyph.m_uv[2], glyph.m_uv[3], glyph.m_layerFloat);
m_color = defaultColor;
adv += glyph.m_advance;
}
2015-11-26 07:35:43 +00:00
static int DoKern(FT_Pos val, const FontAtlas& atlas)
{
val = FT_MulFix(val, atlas.FT_Xscale());
FT_Pos orig_x = val;
/* we scale down kerning values for small ppem values */
/* to avoid that rounding makes them too big. */
/* `25' has been determined heuristically. */
if (atlas.FT_XPPem() < 25)
val = FT_MulDiv(orig_x, atlas.FT_XPPem(), 25);
2015-11-26 23:03:56 +00:00
return FT_PIX_ROUND(val) >> 6;
2015-11-26 07:35:43 +00:00
}
void TextView::typesetGlyphs(const std::string& str, const Zeus::CColor& defaultColor)
2015-11-25 01:46:30 +00:00
{
size_t rem = str.size();
2015-11-26 00:24:01 +00:00
const utf8proc_uint8_t* it = reinterpret_cast<const utf8proc_uint8_t*>(str.data());
utf8proc_int32_t lCh = -1;
m_glyphs.clear();
m_glyphs.reserve(str.size());
int adv = 0;
2015-11-25 01:46:30 +00:00
while (rem)
{
utf8proc_int32_t ch;
utf8proc_ssize_t sz = utf8proc_iterate(it, -1, &ch);
if (sz < 0)
Log.report(LogVisor::FatalError, "invalid UTF-8 char");
2015-11-26 00:24:01 +00:00
if (ch == '\n')
break;
2015-11-25 01:46:30 +00:00
2015-11-26 00:24:01 +00:00
const FontAtlas::Glyph* glyph = m_fontAtlas.lookupGlyph(ch);
if (!glyph)
{
rem -= sz;
it += sz;
continue;
}
if (lCh != -1)
{
2015-11-26 23:03:56 +00:00
adv += DoKern(m_fontAtlas.lookupKern(lCh, ch), m_fontAtlas);
2015-11-26 00:24:01 +00:00
m_glyphs.emplace_back(adv, *glyph, defaultColor);
}
else
m_glyphs.emplace_back(adv, *glyph, defaultColor);
2015-11-25 01:46:30 +00:00
2015-11-26 00:24:01 +00:00
lCh = ch;
2015-11-25 01:46:30 +00:00
rem -= sz;
it += sz;
2015-11-26 07:35:43 +00:00
if (m_glyphs.size() == m_capacity)
break;
2015-11-25 01:46:30 +00:00
}
2015-11-26 00:24:01 +00:00
2015-11-26 07:35:43 +00:00
m_validSlots = 0;
2015-11-25 01:46:30 +00:00
}
2015-11-26 07:35:43 +00:00
void TextView::typesetGlyphs(const std::wstring& str, const Zeus::CColor& defaultColor)
2015-11-25 01:46:30 +00:00
{
2015-11-26 00:24:01 +00:00
wchar_t lCh = -1;
m_glyphs.clear();
m_glyphs.reserve(str.size());
int adv = 0;
for (wchar_t ch : str)
{
if (ch == L'\n')
break;
const FontAtlas::Glyph* glyph = m_fontAtlas.lookupGlyph(ch);
if (!glyph)
continue;
if (lCh != -1)
{
2015-11-26 07:35:43 +00:00
adv += DoKern(m_fontAtlas.lookupKern(lCh, ch), m_fontAtlas);
2015-11-26 00:24:01 +00:00
m_glyphs.emplace_back(adv, *glyph, defaultColor);
}
else
m_glyphs.emplace_back(adv, *glyph, defaultColor);
lCh = ch;
2015-11-26 07:35:43 +00:00
if (m_glyphs.size() == m_capacity)
break;
2015-11-26 00:24:01 +00:00
}
2015-11-26 07:35:43 +00:00
m_validSlots = 0;
2015-11-25 01:46:30 +00:00
}
2015-11-26 07:35:43 +00:00
void TextView::colorGlyphs(const Zeus::CColor& newColor)
2015-11-25 01:46:30 +00:00
{
2015-11-26 07:35:43 +00:00
for (RenderGlyph& glyph : m_glyphs)
glyph.m_color = newColor;
m_validSlots = 0;
2015-11-25 01:46:30 +00:00
}
2015-11-26 07:35:43 +00:00
void TextView::colorGlyphsTypeOn(const Zeus::CColor& newColor, float startInterval, float fadeTime)
2015-11-25 01:46:30 +00:00
{
}
void TextView::think()
{
}
void TextView::draw(boo::IGraphicsCommandQueue* gfxQ)
{
2015-11-26 07:35:43 +00:00
View::draw(gfxQ);
2015-11-26 00:24:01 +00:00
int pendingSlot = 1 << gfxQ->pendingDynamicSlot();
2015-11-26 07:35:43 +00:00
if ((m_validSlots & pendingSlot) == 0)
2015-11-26 00:24:01 +00:00
{
m_glyphBuf->load(m_glyphs.data(), m_glyphs.size() * sizeof(RenderGlyph));
2015-11-26 07:35:43 +00:00
m_validSlots |= pendingSlot;
2015-11-26 00:24:01 +00:00
}
2015-11-26 07:35:43 +00:00
gfxQ->setShaderDataBinding(m_shaderBinding);
2015-11-26 23:03:56 +00:00
gfxQ->setDrawPrimitive(boo::Primitive::TriStrips);
2015-11-26 00:24:01 +00:00
gfxQ->drawInstances(0, 4, m_glyphs.size());
2015-11-25 01:46:30 +00:00
}
2015-11-21 23:45:02 +00:00
}