_font cached transform

This commit is contained in:
2023-09-09 12:31:31 +03:00
parent cad96d8131
commit 26ae90a999

View File

@@ -207,6 +207,7 @@ namespace
// where 'm' is 2D transformation (scale, shear and rotate), 't' is translation
Array<Matrix3x3, InlinedAllocation<64>> TransformLayersStack;
Matrix3x3 TransformCached;
bool TransformIsIdentity;
Array<ClipMask, InlinedAllocation<64>> ClipLayersStack;
Array<Color, InlinedAllocation<64>> TintLayersStack;
@@ -239,24 +240,18 @@ FORCE_INLINE void ApplyTransform(const Float2& value, Float2& result)
void ApplyTransform(const Rectangle& value, RotatedRectangle& result)
{
const RotatedRectangle rotated(value);
Matrix3x3::Transform2DPoint(rotated.TopLeft, TransformCached, result.TopLeft);
Matrix3x3::Transform2DVector(rotated.ExtentX, TransformCached, result.ExtentX);
Matrix3x3::Transform2DVector(rotated.ExtentY, TransformCached, result.ExtentY);
}
FORCE_INLINE Render2DVertex MakeVertex(const Float2& pos, const Float2& uv, const Color& color)
{
Float2 point;
ApplyTransform(pos, point);
return
if (TransformIsIdentity)
{
point,
Half2(uv),
color * TintLayersStack.Peek(),
{ 0.0f, (float)Render2D::Features },
ClipLayersStack.Peek().Mask
};
Matrix3x3::Transform2DPoint(rotated.TopLeft, TransformCached, result.TopLeft);
Matrix3x3::Transform2DVector(rotated.ExtentX, TransformCached, result.ExtentX);
Matrix3x3::Transform2DVector(rotated.ExtentY, TransformCached, result.ExtentY);
}
else
{
Matrix3x3::Transform2DPoint(rotated.TopLeft, TransformCached, result.TopLeft);
Matrix3x3::Transform2DVector(rotated.ExtentX, TransformCached, result.ExtentX);
Matrix3x3::Transform2DVector(rotated.ExtentY, TransformCached, result.ExtentY);
}
}
FORCE_INLINE Render2DVertex MakeVertex(const Float2& point, const Float2& uv, const Color& color, const RotatedRectangle& mask, const Float2& customData)
@@ -283,6 +278,19 @@ FORCE_INLINE Render2DVertex MakeVertex(const Float2& point, const Float2& uv, co
};
}
FORCE_INLINE Render2DVertex MakeVertex(const Float2& pos, const Float2& uv, const Color& color)
{
//return MakeVertex(point, uv, color, ClipLayersStack.Peek().Mask, { 0.0f, (float)Render2D::Features }, TintLayersStack.Peek());
return
{
pos,
Half2(uv),
color * TintLayersStack.Peek(),
{ 0.0f, (float)Render2D::Features },
ClipLayersStack.Peek().Mask
};
}
FORCE_INLINE Render2DVertex MakeTransformedVertex(const Float2& pos, const Float2& uv, const Color& color)
{
Float2 point;
@@ -299,19 +307,37 @@ FORCE_INLINE Render2DVertex MakeTransformedVertex(const Float2& pos, const Float
};
}
FORCE_INLINE Render2DVertex MakeTransformedVertex(const Float2& pos, const Float2& uv, const Color& colorTint, const RotatedRectangle& mask, const Float2& customData)
{
Float2 point;
ApplyTransform(pos, point);
//return MakeVertex(point, uv, color, ClipLayersStack.Peek().Mask, { 0.0f, (float)Render2D::Features }, TintLayersStack.Peek());
return
{
point,
Half2(uv),
colorTint,
customData,
mask
};
}
void WriteTri(const Float2& p0, const Float2& p1, const Float2& p2, const Float2& uv0, const Float2& uv1, const Float2& uv2, const Color& color0, const Color& color1, const Color& color2)
{
Render2DVertex tris[3];
tris[0] = MakeVertex(p0, uv0, color0);
tris[1] = MakeVertex(p1, uv1, color1);
tris[2] = MakeVertex(p2, uv2, color2);
VB.Write(tris, sizeof(tris));
//Render2DVertex tris[3];
Render2DVertex* tris = (Render2DVertex*)VB.WriteReserve(sizeof(Render2DVertex) * 3);
tris[0] = MakeTransformedVertex(p0, uv0, color0);
tris[1] = MakeTransformedVertex(p1, uv1, color1);
tris[2] = MakeTransformedVertex(p2, uv2, color2);
//VB.Write(tris, sizeof(tris));
uint32 indices[3];
//uint32 indices[3];
uint32* indices = (uint32*)IB.WriteReserve(sizeof(uint32) * 3);
indices[0] = VBIndex + 0;
indices[1] = VBIndex + 1;
indices[2] = VBIndex + 2;
IB.Write(indices, sizeof(indices));
//IB.Write(indices, sizeof(indices));
VBIndex += 3;
IBIndex += 3;
@@ -359,10 +385,24 @@ void WriteRect(const Rectangle& rect, const Color& color, const Float2& uvUpperL
{
Render2DVertex* quad = (Render2DVertex*)VB.WriteReserve(sizeof(Render2DVertex) * 4);
//Render2DVertex quad[4];
quad[0] = MakeTransformedVertex(rect.GetBottomRight(), uvBottomRight, color);
quad[1] = MakeTransformedVertex(rect.GetBottomLeft(), Float2(uvUpperLeft.X, uvBottomRight.Y), color);
quad[2] = MakeTransformedVertex(rect.GetUpperLeft(), uvUpperLeft, color);
quad[3] = MakeTransformedVertex(rect.GetUpperRight(), Float2(uvBottomRight.X, uvUpperLeft.Y), color);
if (TransformIsIdentity)
{
RotatedRectangle mask = ClipLayersStack.Peek().Mask;
Float2 customData = { 0.0f, (float)Render2D::Features };
Color colorTint = color * TintLayersStack.Peek();
quad[0] = MakeVertex(rect.GetBottomRight(), uvBottomRight, colorTint, mask, customData);
quad[1] = MakeVertex(rect.GetBottomLeft(), Float2(uvUpperLeft.X, uvBottomRight.Y), colorTint, mask, customData);
quad[2] = MakeVertex(rect.GetUpperLeft(), uvUpperLeft, colorTint, mask, customData);
quad[3] = MakeVertex(rect.GetUpperRight(), Float2(uvBottomRight.X, uvUpperLeft.Y), colorTint, mask, customData);
}
else
{
quad[0] = MakeTransformedVertex(rect.GetBottomRight(), uvBottomRight, color);
quad[1] = MakeTransformedVertex(rect.GetBottomLeft(), Float2(uvUpperLeft.X, uvBottomRight.Y), color);
quad[2] = MakeTransformedVertex(rect.GetUpperLeft(), uvUpperLeft, color);
quad[3] = MakeTransformedVertex(rect.GetUpperRight(), Float2(uvBottomRight.X, uvUpperLeft.Y), color);
}
//VB.Write(quad, sizeof(quad));
//uint32 indices[6];
@@ -711,6 +751,7 @@ void Render2D::Begin(GPUContext* context, GPUTextureView* output, GPUTextureView
TransformLayersStack.Clear();
TransformLayersStack.Push(defaultTransform);
TransformCached = defaultTransform;
TransformIsIdentity = true;
// Initialize default clip mask
const Rectangle defaultBounds(viewport.Location, viewport.Size);
@@ -840,6 +881,7 @@ void Render2D::PushTransform(const Matrix3x3& transform)
// Push it
TransformLayersStack.Push(finalTransform);
TransformCached = TransformLayersStack.Peek();
TransformIsIdentity = TransformCached.IsIdentity();
}
void Render2D::PeekTransform(Matrix3x3& transform)
@@ -854,6 +896,7 @@ void Render2D::PopTransform()
ASSERT(TransformLayersStack.HasItems());
TransformLayersStack.Pop();
TransformCached = TransformLayersStack.Peek();
TransformIsIdentity = TransformCached.IsIdentity();
}
void OnClipScissors()
@@ -1468,7 +1511,10 @@ void Render2D::DrawRectangle(const Rectangle& rect, const Color& color1, const C
RENDER2D_CHECK_RENDERING_STATE;
const auto& mask = ClipLayersStack.Peek().Mask;
thickness *= (TransformCached.M11 + TransformCached.M22 + TransformCached.M33) * 0.3333333f;
if (TransformIsIdentity)
thickness *= (TransformCached.M11 + TransformCached.M22 + TransformCached.M33) * 0.3333333f;
else
thickness *= (TransformCached.M11 + TransformCached.M22 + TransformCached.M33) * 0.3333333f;
Float2 points[5];
ApplyTransform(rect.GetUpperLeft(), points[0]);
@@ -1484,8 +1530,8 @@ void Render2D::DrawRectangle(const Rectangle& rect, const Color& color1, const C
colors[3] = color4;
colors[4] = colors[0];
Render2DVertex v[4];
uint32 indices[6];
Render2DVertex* v = (Render2DVertex*)VB.WriteReserve(sizeof(Render2DVertex) * (4 * 7));
uint32* indices = (uint32*)IB.WriteReserve(sizeof(uint32) * (4 * 9));
Float2 p1t, p2t;
Color c1t, c2t;
@@ -1517,7 +1563,12 @@ void Render2D::DrawRectangle(const Rectangle& rect, const Color& color1, const C
v[1] = MakeVertex(p1t + up, Float2::UnitX, c1t, mask, { thickness, (float)Features });
v[2] = MakeVertex(p1t - up, Float2::Zero, c1t, mask, { thickness, (float)Features });
v[3] = MakeVertex(p2t - up, Float2::Zero, c2t, mask, { thickness, (float)Features });
VB.Write(v, sizeof(Render2DVertex) * 4);
v++;
v++;
v++;
v++;
//VB.Write(v, sizeof(Render2DVertex) * 4);
indices[0] = VBIndex + 0;
indices[1] = VBIndex + 1;
@@ -1525,7 +1576,13 @@ void Render2D::DrawRectangle(const Rectangle& rect, const Color& color1, const C
indices[3] = VBIndex + 2;
indices[4] = VBIndex + 3;
indices[5] = VBIndex + 0;
IB.Write(indices, sizeof(uint32) * 6);
indices++;
indices++;
indices++;
indices++;
indices++;
indices++;
//IB.Write(indices, sizeof(uint32) * 6);
VBIndex += 4;
IBIndex += 6;
@@ -1536,15 +1593,21 @@ void Render2D::DrawRectangle(const Rectangle& rect, const Color& color1, const C
v[0] = MakeVertex(p2t - up, Float2::Zero, c2t, mask, { tmp, (float)Features });
v[1] = MakeVertex(p2t + right, Float2::Zero, c2t, mask, { tmp, (float)Features });
v[2] = MakeVertex(p2t, Float2(0.5f, 0.0f), c2t, mask, { tmp, (float)Features });
VB.Write(v, sizeof(Render2DVertex) * 4);
//v[3] = v[-1];
v++;
v++;
v++;
//VB.Write(v, sizeof(Render2DVertex) * 4);
indices[0] = VBIndex + 1;
indices[1] = VBIndex + 2;
indices[2] = VBIndex + 0;
indices++;
indices++;
indices++;
//IB.Write(indices, sizeof(uint32) * 3);
IB.Write(indices, sizeof(uint32) * 3);
VBIndex += 4;
VBIndex += 3;
IBIndex += 3;
p1t = p2t;
@@ -1765,15 +1828,17 @@ void DrawLineCap(const Float2& capOrigin, const Float2& capDirection, const Floa
{
const auto& mask = ClipLayersStack.Peek().Mask;
Render2DVertex v[5];
//Render2DVertex v[5];
Render2DVertex* v = (Render2DVertex*)VB.WriteReserve(sizeof(Render2DVertex) * 5);
v[0] = MakeVertex(capOrigin, Float2(0.5f, 0.0f), color, mask, { thickness, (float)Render2D::Features });
v[1] = MakeVertex(capOrigin + capDirection + up, Float2::Zero, color, mask, { thickness, (float)Render2D::Features });
v[2] = MakeVertex(capOrigin + capDirection - up, Float2::Zero, color, mask, { thickness, (float)Render2D::Features });
v[3] = MakeVertex(capOrigin + up, Float2::Zero, color, mask, { thickness, (float)Render2D::Features });
v[4] = MakeVertex(capOrigin - up, Float2::Zero, color, mask, { thickness, (float)Render2D::Features });
VB.Write(v, sizeof(v));
//VB.Write(v, sizeof(v));
uint32 indices[9];
//uint32 indices[9];
uint32* indices = (uint32*)IB.WriteReserve(sizeof(uint32) * 9);
indices[0] = VBIndex + 0;
indices[1] = VBIndex + 3;
indices[2] = VBIndex + 1;
@@ -1783,7 +1848,7 @@ void DrawLineCap(const Float2& capOrigin, const Float2& capDirection, const Floa
indices[6] = VBIndex + 0;
indices[7] = VBIndex + 2;
indices[8] = VBIndex + 4;
IB.Write(indices, sizeof(indices));
//IB.Write(indices, sizeof(indices));
VBIndex += 5;
IBIndex += 9;
@@ -1796,7 +1861,10 @@ void DrawLines(const Float2* points, int32 pointsCount, const Color& color1, con
ASSERT(points && pointsCount >= 2);
const auto& mask = ClipLayersStack.Peek().Mask;
thickness *= (TransformCached.M11 + TransformCached.M22 + TransformCached.M33) * 0.3333333f;
if (TransformIsIdentity)
thickness *= (TransformCached.M11 + TransformCached.M22 + TransformCached.M33) * 0.3333333f;
else
thickness *= (TransformCached.M11 + TransformCached.M22 + TransformCached.M33) * 0.3333333f;
Render2DDrawCall& drawCall = DrawCalls.AddOne();
drawCall.StartIB = IBIndex;