From fb6fadbbb79d90c26eb857061083bf57f4c7575b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 14 Nov 2021 10:24:54 -0800 Subject: [PATCH] softgpu: Fast path rectangles as fans. Some games, such as Legend of Heroes III, use fans instead of strips. --- GPU/Math3D.h | 4 +++ GPU/Software/RasterizerRectangle.cpp | 39 ++++++++++++++++++++++++++++ GPU/Software/RasterizerRectangle.h | 1 + GPU/Software/TransformUnit.cpp | 18 ++++++++++++- 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/GPU/Math3D.h b/GPU/Math3D.h index b8e26f58b3..ff05280c43 100644 --- a/GPU/Math3D.h +++ b/GPU/Math3D.h @@ -289,6 +289,10 @@ public: *this = *this / f; } + bool operator ==(const Vec3 &other) const { + return x == other.x && y == other.y && z == other.z; + } + T Length2() const { return x*x + y*y + z*z; diff --git a/GPU/Software/RasterizerRectangle.cpp b/GPU/Software/RasterizerRectangle.cpp index 92573ad4fd..ef2438083a 100644 --- a/GPU/Software/RasterizerRectangle.cpp +++ b/GPU/Software/RasterizerRectangle.cpp @@ -362,6 +362,45 @@ bool DetectRectangleFromThroughModeStrip(const VertexData data[4]) { return false; } +bool DetectRectangleFromThroughModeFan(const VertexData *data, int c, int *tlIndex, int *brIndex) { + // Color and Z must be flat. + for (int i = 1; i < c; ++i) { + if (!(data[i].color0 == data[0].color0)) + return false; + if (!(data[i].screenpos.z == data[0].screenpos.z)) + return false; + } + + // Check for the common case: a single TL-TR-BR-BL. + if (c == 4) { + const auto &tl = data[0].screenpos, &tr = data[1].screenpos; + const auto &bl = data[3].screenpos, &br = data[2].screenpos; + if (tl.x == bl.x && tr.x == br.x && tl.y == tr.y && bl.y == br.y) { + // Looking like yes. Set TL/BR based on y order first... + *tlIndex = tl.y > bl.y ? 2 : 0; + *brIndex = tl.y > bl.y ? 0 : 2; + // And if it's horizontally flipped, trade to the actual TL/BR. + if (tl.x > tr.x) { + *tlIndex ^= 1; + *brIndex ^= 1; + } + + // Do we need to think about rotation? + if (!gstate.isTextureMapEnabled()) + return true; + + const auto &textl = data[*tlIndex].texturecoords, &textr = data[*tlIndex ^ 1].texturecoords; + const auto &texbl = data[*brIndex ^ 1].texturecoords, &texbr = data[*brIndex].texturecoords; + + if (textl.x == texbl.x && textr.x == texbr.x && textl.y == textr.y && texbl.y == texbr.y) { + // Okay, the texture is also good, but let's avoid rotation issues. + return textl.y < texbr.y && textl.x < texbr.x; + } + } + } + + return false; +} } // namespace Rasterizer diff --git a/GPU/Software/RasterizerRectangle.h b/GPU/Software/RasterizerRectangle.h index c3487340eb..2489e45d60 100644 --- a/GPU/Software/RasterizerRectangle.h +++ b/GPU/Software/RasterizerRectangle.h @@ -17,4 +17,5 @@ namespace Rasterizer { bool RectangleFastPath(const VertexData &v0, const VertexData &v1); bool DetectRectangleFromThroughModeStrip(const VertexData data[4]); + bool DetectRectangleFromThroughModeFan(const VertexData *data, int c, int *tlIndex, int *brIndex); } diff --git a/GPU/Software/TransformUnit.cpp b/GPU/Software/TransformUnit.cpp index 3a88ad8062..66d13a69ec 100644 --- a/GPU/Software/TransformUnit.cpp +++ b/GPU/Software/TransformUnit.cpp @@ -445,7 +445,7 @@ void TransformUnit::SubmitPrimitive(void* vertices, void* indices, GEPrimitiveTy // If index count == 4, check if we can convert to a rectangle. // This is for Darkstalkers (and should speed up many 2D games). - if (vertex_count == 4 && gstate.isModeThrough()) { + if (data_index == 0 && vertex_count == 4 && gstate.isModeThrough()) { for (int vtx = 0; vtx < 4; ++vtx) { if (indices) { vreader.Goto(ConvertIndex(vtx) - index_lower_bound); @@ -517,6 +517,22 @@ void TransformUnit::SubmitPrimitive(void* vertices, void* indices, GEPrimitiveTy data_index++; start_vtx = 1; } + if (data_index == 1 && vertex_count == 4 && gstate.isModeThrough()) { + for (int vtx = start_vtx; vtx < vertex_count; ++vtx) { + if (indices) { + vreader.Goto(ConvertIndex(vtx) - index_lower_bound); + } else { + vreader.Goto(vtx); + } + data[vtx] = ReadVertex(vreader, outside_range_flag); + } + + int tl = -1, br = -1; + if (Rasterizer::DetectRectangleFromThroughModeFan(data, vertex_count, &tl, &br)) { + Clipper::ProcessRect(data[tl], data[br]); + break; + } + } outside_range_flag = false; for (int vtx = start_vtx; vtx < vertex_count; ++vtx) {